I have 2 header files that contain 2 classes. Each class is dependent on other, like so:
// class1.h
#include "class2.h"
class ClassOne {
ClassTwo* c2;
};
// class2.h
#include "class1.h"
class ClassTwo {
ClassOne* c1;
};
I expected the code not to compile, so I added a forward declaration to one of the headers:
// class1.h
#include "class2.h"
class ClassTwo;
class ClassOne {
ClassTwo* c2;
};
But sadly that isn’t working, either. I keep getting compiler errors for “use of undefined type ‘ClassTwo'”.
I know I can just combine them into one header file and they will work, but in reality they are both really big classes and I really would prefer they have their own header file for organization purposes.
Is there a way to get around this?
Thanks,
Alex
You added the forward declaration but didn’t remove the
include. Do that and you’re golden.Actually, remove both includes, and replace them with forward declarations.
Note that cases where circular dependencies are actually required are few and far apart, so at least review the design.