In C++, I have a problem with circular dependencies / incomplete types. The situation is as follows:
Stuffcollection.h
#include "Spritesheet.h";
class Stuffcollection {
public:
void myfunc (Spritesheet *spritesheet);
void myfuncTwo ();
};
Stuffcollection.cpp
void Stuffcollection::myfunc(Spritesheet *spritesheet) {
unsigned int myvar = 5 * spritesheet->spritevar;
}
void myfunc2() {
//
}
Spritesheet.h
#include "Stuffcollection.h"
class Spritesheet {
public:
void init();
};
Spritesheet.cpp
void Spritesheet::init() {
Stuffcollection stuffme;
myvar = stuffme.myfuncTwo();
}
- If I keep the includes as shown above, I get the compiler error
spritesheet has not been declared
in Stuffcollection.h (line 4 in the above). I understand this to be due to a circular dependency. - Now if I change
#include "Spritesheet.h"
to the Forward Declarationclass Spritesheet;
in Stuffcollection.h, I get the compiler errorinvalid use of incomplete type 'struct Spritesheet'
in Stuffcollection.cpp (line 2 in the above). - Similarly, if I change
#include "Stuffcollection.h"
toclass Stuffcollection;
in Spritesheet.h, I get the compiler erroraggregate 'Stuffcollection stuffme' has incomplete type and cannot be defined
in Spritesheet.cpp (line 2 in the above).
What can I do to solve this problem?
#include
in the .cpp files. – Mabuse