I have four C++ files, two headers and two cpp. The headers are properly guarded and in the first I use a forward declaration like this:
//A.h
class B;
class A {
public:
A();
void doSomething(B* b);
};
and some implementation:
void A::doSomething(B* b){
b->add();
}
the other header is as follows:
#include "A.h"
class B {
public:
B();
void add();
};
and
void B::add(){
cout << "B is adding" << endl;
}
I get the "member access into incomplete type B" error and I'm stuck in there. I need to use a of Bs into A and each B must have a pointer to its "owner", an instance of A. What can I do to solve this situation.
Thanks in advance
You need to have the definition for class B before you can use the
doSomething(B* b)
method, so I think instead of:you would need to do something like:
declaring it after so the compiler has the information it needs about class B (memory allocation, etc).