I have a situation as such
A.h
#ifndef _CLASSA
#define _CLASSA
class B;
class A {
virtual void addTo(B*) {}
};
#endif
B.h
#ifndef _CLASSB
#define _CLASSB
#include "A.h"
class B : public A {
B();
void addTo(B* b) {
// blah blah blah
}
};
#endif
B.cpp
#include "B.h"
B::B() : A() {}
main.cpp
#include "B.h"
int main() {
A* b = new B();
B* d = new B();
b->addTo(d);
}
The project won’t compile. If I forward declare B in the A header, the compiler complains about the expectation of a class in B.h. If I include the B.h header in A.h, the compiler can’t resolve the base class. Is this possible?
Yes, include
A.hinB.hand forward declareBinA.h.The way you have the code now it should work. But I suspect you’re actually using
Binside the methodaddTo, in which case a forward declaration is not enough. You need to separate the implementation to an implementation file and includeBthere.EDIT: As DeadMg pointed out,
class B : class A {isn’t valid syntax, you probably wantclass B : Aorclass B : public A.