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
The
B.cppfile, despite named similarly, does not implicitly includeB.h. You have to include theB.hfrom both implementations. Also, if you want to include bothA.handB.h, create the include guards