Declaring private pointers in class
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?
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.
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.
i.e this is wrong (C++ != Java):
while this is fine:
-me
[edited by - Palidine on March 26, 2004 4:07:05 PM]
[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]
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 filesomeClass::someClass(){ // now you can initialize the pointer pSomething = new sometype();}
You didn't give any code but according to the error message you'are doing someting like this:
Instead, you should do it this way:
EDIT: Wow, everyone saying the same thing and everyone was faster than me
[edited by - nonpop on March 26, 2004 4:09:28 PM]
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]
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]
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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement