I have the following program in which I have 2 classes, namely Class A and Class B in which each class has instance of another class declared as a private member.
FYI, for one class I'm using dynamic allocation (pointer instance) and in the other class I'm using automatic allocation (normal instance).
(Program.h)
class A;
class B;
class A
{
A();
~A();
public:
void InitA();
void funcA;
private:
B b; // ERROR! on code compilation: "A::b uses undefined class B"
}
class B
{
B();
~B();
public:
void InitB();
void funcB;
private:
A *a;
}
</code>
(Program.cpp)
B::B() : a(new A()) { } B::~B() { SAFE_DELETE (a); } B::InitB() { a->funcA(); // works beautifully! } A::A() { } A::~A() { } A::InitA() { b.funcB(); // DUE TO ERROR, code never compiles so I don't see this. }
ERROR: "A::b uses undefined class B"
Now, I've deliberately not declared a pointer instance of class B in class A, which I believe should not be the reason for this error. This error is probably because I'm not doing forward declaration properly.