Error when creating class reference to an instance of itself

Y

Yngve

Hi!

I am trying to make two pointers at instances of the same class wich is
beeing defined. But i get the following error from the compiler (MVC7):
--------------------

c:\Utv\Mana\BinaryTree.h(8) : error C2460: 'BinaryTree::Node::right' : uses
'Bin
aryTree::Node', which is being defined
c:\Utv\Mana\BinaryTree.h(6) : see declaration of 'BinaryTree::Node'



Code below this line: (BinaryTree.h)
//-------------------
class Node; // To tell the compiler that Node exists

class BinaryTree
{
class Node
{
int number;
Node* left, right; // Here is the errorenous line
};
};

// I would really appreciate if someone could help me out here. I thought
that i took
// care of the problem in the first line of the header file (but i didn´t).
 
R

Rolf Magnus

Yngve said:
Hi!

I am trying to make two pointers at instances of the same class wich
is beeing defined. But i get the following error from the compiler
(MVC7): --------------------

c:\Utv\Mana\BinaryTree.h(8) : error C2460: 'BinaryTree::Node::right' :
uses 'Bin
aryTree::Node', which is being defined
c:\Utv\Mana\BinaryTree.h(6) : see declaration of
'BinaryTree::Node'



Code below this line: (BinaryTree.h)
//-------------------
class Node; // To tell the compiler that Node exists

This is another Node class than the one in BinaryTree.
class BinaryTree
{
class Node
{
int number;
Node* left, right; // Here is the errorenous line

'left' is a pointer to Node, 'right' is a Node. I guess you wanted two
pointers:

Node* left, * right;

or better (why it's better, you just learned) in two lines:

Node* left;
Node* right;
 
A

Andrey Tarasevich

Yngve said:
I am trying to make two pointers at instances of the same class wich is
beeing defined. But i get the following error from the compiler (MVC7):
--------------------

c:\Utv\Mana\BinaryTree.h(8) : error C2460: 'BinaryTree::Node::right' : uses
'Bin
aryTree::Node', which is being defined
c:\Utv\Mana\BinaryTree.h(6) : see declaration of 'BinaryTree::Node'

Code below this line: (BinaryTree.h)
//-------------------
class Node; // To tell the compiler that Node exists

class BinaryTree
{
class Node
{
int number;
Node* left, right; // Here is the errorenous line
};
};

// I would really appreciate if someone could help me out here. I thought
that i took
// care of the problem in the first line of the header file (but i didn´t).

The first line declares class '::Node' (i.e. global namespace 'Node').
The erroneous line refers to 'BinaryTree::Node', which has absolutely no
relation to global '::Node'.

The forward declaration is completely unnecessary in this case, since
inside 'BinaryTree::Node' definition the compiler already knows that
'BinaryTree::Node' exists. Even though it is an incomplete type at that
time, you still can declare pointers to that type without any problems.
Your code does not compile because 'right' declares an object of
incomplete type 'BinaryTree::Node', not a pointer to 'BinaryTree::Node'.

Try this

// No forward declarations of 'Node' necessary

class BinaryTree
{
class Node
{
int number;
Node *left, *right; // Note the additional '*'
};
};

If you prefer to stick '*'s together with the type name in pointer
declarations, you should probably avoid declaring more than one object
in one declaration statement

class BinaryTree
{
class Node
{
int number;
Node* left;
Node* right;
};
};
 
Y

Yngve

Node* left, right; // Here is the errorenous line
'left' is a pointer to Node, 'right' is a Node. I guess you wanted two
pointers:

Node* left, * right;

Ops, thanks!
 
E

E. Robert Tisdale

Yngve said:
I am trying to make two pointers
at instances of the same class which is being defined.
But I get the following error from the compiler (MVC7):
--------------------

c:\Utv\Mana\BinaryTree.h(8) : error C2460:
'BinaryTree::Node::right' : uses'BinaryTree::Node',
which is being defined
c:\Utv\Mana\BinaryTree.h(6) : see declaration of 'BinaryTree::Node'



Code below this line: (BinaryTree.h)
//-------------------
class Node; // To tell the compiler that Node exists

class BinaryTree { private:
class Node {
private:
// representation
Node* Left;
Node* Right; // Note that this is a *pointer* too.
int number;
public:
// functions, operators and constructors
// representation
Node* Root; // You need a pointer to the root node.
public:
// functions, operators and constructors
 
N

Nicolas Weidmann

Yngve said:
Hi!

I am trying to make two pointers at instances of the same class wich is
beeing defined. But i get the following error from the compiler (MVC7):
--------------------

c:\Utv\Mana\BinaryTree.h(8) : error C2460: 'BinaryTree::Node::right' : uses
'Bin
aryTree::Node', which is being defined
c:\Utv\Mana\BinaryTree.h(6) : see declaration of 'BinaryTree::Node'



Code below this line: (BinaryTree.h)
//-------------------
class Node; // To tell the compiler that Node exists

class BinaryTree
{
class Node
{
int number;
Node* left, right; // Here is the errorenous line
};
};

// I would really appreciate if someone could help me out here. I thought
that i took
// care of the problem in the first line of the header file (but i didn´t).

Node* left, right;

Does not mean 2 pointers named "left" and "right" on a Node.
It means 1 pointer named "left" on a Node and a Node named "right".

The correc thing is:

Node *left, *right;

or:

Node *left;
Node *right;

Have fun.

Nicolas
 
R

Ron Samuel Klatchko

class Node; // To tell the compiler that Node exists
class BinaryTree
{
class Node
{
int number;
Node* left, right; // Here is the errorenous line
};
};

The problem is that the pointer modifier only applies to a single
variable and not to all the variables. So:
Node* left, right;

is the equivalent of:
Node* left;
Node right;

If you really want to put that in a single line, you can use:
Node* left, *right;

Personally, I advise against declaring multiple variables on a single
line as this weirdness of the type declaration system trips up lots of
people. Declare only one variable at a time and you won't confuse
yourself or others:
Node* left;
Node* right;

samuel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top