Yngve wrote:
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;
};
};
--
Best regards,
Andrey Tarasevich