← Back to topics
Topic

Tree

t
tgudlek

struct node;

struct edge{
node to;
char letter;
inline friend bool operator < ( const edge &a, const edge &b ){
return a.letter < b.letter;
}
};


struct node{
set <edge> edges;
int val;
node( void ){
val = 0;
}
};



How to make this work?
C
Cotizo
What you are asking about is called "mutual class definition". Here are a couple of sites that present a solution to your problem:
http://www.codeguru.com/forum/showthread.php?t=383253
http://photon.poly.edu/~hbr/cs903-F00/lib_design/notes/advanced.html#CyclicTemplates

I think the latter is the best one. Try something like this:
template <class Graph>
struct edge {
typedef typename Graph::node node;
node to;
char letter;
inline friend bool operator< ( const edge &a, const edge &b ) {
return a.letter < b.letter;
}
};
template <class Graph>
struct node {
typedef typename Graph::edge edge;
set <edge> edges;
int val;
node( void ) {
val = 0;
}
};
t
tgudlek
What about a dynamic list? How can I create a "node" somewhere in memory and save only a pointer of it to some variable? Of course, I don't want that node to disappear when a function closes.