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

Posted on 2003-03-11
Medium Priority
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++).

Question by:hbean
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 45

Accepted Solution

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

class Root;  // This declares a forward reference!

LVL 45

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,

Author Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

771 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