473,473 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Error when creating class reference to an instance of itself

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).
Jul 22 '05 #1
7 2551
Yngve wrote:
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;

};
};

// 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).


Jul 22 '05 #2
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

Jul 22 '05 #3
> > 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!
Jul 22 '05 #4
Thanks for the answer and your time.
Jul 22 '05 #5
Yngve wrote:
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 };

// 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).


Jul 22 '05 #6
Yngve wrote:
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

Jul 22 '05 #7
> 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
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Steve Jorgensen | last post by:
Here is some code to generate code for raising and getting information about custom errors in an application. Executing the GenerateXyzErrDefs procedure generates the code. <tblXyzError>...
6
by: Laura Carr | last post by:
I have been getting the following error fro an event in an rpx.cs file. The error is as follows: An object reference is required for the nonstatic field, method, or property...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
4
by: Eric A. Johnson | last post by:
For the following code: ' return String representation of CTriangleShape Public Overrides Function ToString() As String ' use MyBase reference to return CShape String Return...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
4
by: rushikesh.joshi | last post by:
Hi All, I have created my own WebControl and want to add it in my aspx page at runtime. it's compiling perfectly, but when i m going to execute, it gives me error of "Object reference not set...
2
by: cfriedalek | last post by:
Sorry for the vague subject. Not sure what the right terminology is. How can I use an instance's data by reference to the instance name, not the instance attribute? OK the question is probably...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
0
by: creatiive | last post by:
hi, i have a program that when executed, creates an instance of itself and then runs a method (that has been written in its base class). This works fine, and looks like this; public...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.