PDA

View Full Version : C++ Related Classes Problems



Trinithis
10-15-2007, 12:52 AM
I can't get the following code to compile:


class Foo {
public:
Bar* bar;
Foo() {
bar = NULL;
}
};

class Bar {
public:
Foo* foo;
Bar() {
foo = NULL;
}
};

How can I achieve this effect?

Twey
10-15-2007, 12:58 AM
You need to use a forward declaration:
class Bar;

class Foo {
public:
Bar* bar;
Foo() {
bar = 0;
}
};

class Bar {
public:
Foo* foo;
Bar() {
foo = 0;
}
};

Trinithis
10-15-2007, 01:03 AM
Thanks. I tried that a first and thought I was doing something wrong because it didn't work. As it turns out, I am working with generics, and I needed a little extra:

template <class T> class Iterator;
Seems to work. For now . . .