Archived

This topic is now archived and is closed to further replies.

Declaring private pointers in class

This topic is 5014 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m trying to write a link list program, and within my Node class, I want to declare pointers to other nodes. The problem is that it won''t let me declare them how I want. When trying to place the declarations in the Private: section of the header file, I get the compiler error only const static integral data members can be initialized inside a class or struct (This is using VC++ 2003) As such, I''m forced to declare the pointers in the CPP file, just before the functions, which I believe makes the pointers global. Does anybody know how or where I can declare the pointers to make them private?

Share this post


Link to post
Share on other sites
It''s easiest if you try posting code showing what you want to do.

That being said, you''re probably trying to initialize a member variable outside of a constructor, which is not valid C++. If you want to give a member variable an initial value, you should do it in the constructor.

Share this post


Link to post
Share on other sites
i.e this is wrong (C++ != Java):

[source]
class foo
{
private:
foo * myFoo = NULL;
};



while this is fine:

class foo
{
public:
foo()
{
myFoo = NULL;
}
private:
foo * myFoo;
};


-me

[edited by - Palidine on March 26, 2004 4:07:05 PM]

Share this post


Link to post
Share on other sites
looking at the error you have, the problem does not seem to be the declaration, but you seem to initialize the pointer in the declaration.


class someClass
{
public:
someClass();
// ...

private:
// this is bad, you can''t initialize a variable in a class

// declaration

sometype *pSomething = new sometype();

// this is the way to do it

sometype *pSomething;
};

// in your .cpp file

someClass::someClass()
{
// now you can initialize the pointer

pSomething = new sometype();
}

Share this post


Link to post
Share on other sites
You didn't give any code but according to the error message you'are doing someting like this:

class c_node
{
private:
c_node* other_node = NULL; // that's it, you're trying to initializing the variable
};

Instead, you should do it this way:

In header:
class c_node
{
public:
c_node ();

private:
c_node* other_node;
};

In cpp file:
c_node::c_node () : other_node(NULL) // this initializes the variable to NULL when the c_node is instantiated
{
// ...
}


EDIT: Wow, everyone saying the same thing and everyone was faster than me


[edited by - nonpop on March 26, 2004 4:09:28 PM]

Share this post


Link to post
Share on other sites
that is because the way the compiler initializes variables in VS.net. You can not init a pointer like so either.

class x{
public:
x();
private:
long *Ptr;
}

x::x(): Ptr(null)
{

}


something wierd about VS.net

--
What are you nutz?

I have nothing to say to your unevolved little brain. The more I say gives you more weapons to ask stupid questions.


Sorry left the variable out and the code smiles



[edited by - afterburn on March 26, 2004 4:16:50 PM]

Share this post


Link to post
Share on other sites