I have three classes: a TopClass which contains an instance of a BottomClass pointer. The BottomClass contains a pointer to a HelperClass. The HelperClass keeps a pointer to the TopClass. Circular dependency pops up and a forward declaration is needed in the HelperClass.
All of this is illustrated with the following code snippets:
#include "BottomLevel.h"
namespace foo
{
class TopLevel
{
private:
BottomLevel* item;
};
}
–
#include "HelperClass.h"
namespace foo
{
class BottomLevel
{
private:
HelperClass* item;
};
}
–
class TopLevel; // forward declaration here
namespace foo
{
class HelperClass
{
public
HelperClass(TopLevel* item);
};
}
The issue comes when trying to do things in the implementation file. If I #include "TopClass.h" in the cpp file, I get compilation errors stating “overloaded member function not found — use of undefined type ‘TopLevel‘” (ERRORS C2511 and C2027).
Then, if I don’t do the #include I’m still left with C2027 errors because I try to use the forward declared type.
I just know there is a method to fix this, I’m sure I’ve done it before, but I can’t for the life of me remember what I’m supposed to do. Any help please?
The problem is that you’re forward declaring
TopLeveloutside thefoonamespace so the compiler is never finding the classfoo::TopLevel.Try moving the forward declaration of
TopLevelinside thefoonamespace.