Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Object Problem...(newbie stuff)...

Posted on 2003-03-11
3
Medium Priority
?
188 Views
Last Modified: 2010-04-01
Afternoon all...

Im trying to create a class like such:

class Node{
...
Root *root;
...
}

class Root{
...
Node *node;//actually a vector, but none the less..
...
}

The compiler wont let me place a pointer to root into my node object because root hasnt been declared yet...and if I switch the order the problem will be just the opposite.  Is this an impossiblity w/ C++?  (I'm an experience Java programmer, new to c/c++).

Thanks!
-hhb
0
Comment
Question by:hbean
  • 2
3 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 1000 total points
ID: 8113256
At the top of this sequence of code enter:


class Root;  // This declares a forward reference!


Kdo
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8113443
One of the things about C, C++, PASCAL and a lot of other languages is that you can only create references to objects that the compiler knows about.  (Personally, I'd prefer that the compiler resolves some of this later.  After all, a pointer to a class takes up exactly the same space as a pointer to an int so it's not a matter of the type of object being indefinite.)

But I digress.


For complex types (struct, typedef struct, and class) it is often desireable to have links in both directions.  But as you've discovered, the compiler won't let you define have a pointer to an object for which it has no definition.

Hence, the forward reference.

class Second;

class First
{
  Second *SecondPtr;
};

class Second
{
  First *FirstPtr;
}


Good Luck,
Kent
0
 

Author Comment

by:hbean
ID: 8113546
Awesome.  I knew it was easy LOL.  Thanks!
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question