mutually recursive classes

Started by
4 comments, last by UK_Gamer 19 years, 11 months ago
Hi, I have successfully implemented a basic A* search program in Java, and am now trying to port it to C++. The problem is my node and nodelist classes in java. Node class holds the data at that node, and a nodelist of nodes connected to this node. Nodelist class holds a nodelist of nodes connected to this list, as well as the node at the top of this list. Having two classes use each other is fine in java, but in C++, one class can''t compile until the other one is compiled, and vice-versa. Is there a way round this problem?
Advertisement
Yes.

class NodeList;

class Node
{
...
};

class NodeList
{
...
};
Forward declarations only work if you''re using pointers, or otherwise don''t need the actual definition of the class, only the name.
Yes, but he needs to be using a pointer in at least one of the classes anyway.
quote:Original post by psykr
Forward declarations only work if you''re using pointers, or otherwise don''t need the actual definition of the class, only the name.


For it to be logically possible (realizing that we don''t use quantum computers) for it to work in any language (he said it worked in java) one of the classes must be restricted to merely having a pointer to the other.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Pointers are the solution thanks.

(Java only uses pointers I believe which is why it worked originally)

This topic is closed to new replies.

Advertisement