I’m getting a compiler error with this header file:
#ifndef GAME1_H
#define GAME1_H
#include "GLGraphics.h"
#include "DrawBatch.h"
class GameComponent;
class Game1 {
private:
GLGraphics graphics;
GameComponent components;
void updateDelegates();
void Run();
};
class GameComponent {
private:
static int index;
protected:
Game1 game;
public:
GameComponent();
GameComponent(Game1 game);
void Update(int);
void Dispose();
};
class DrawableGameComponent: public GameComponent
{
private:
GLGraphics graphics;
DrawBatch drawBatch;
public:
DrawableGameComponent();
DrawableGameComponent(Game1);
void Draw();
};
#endif
I see the problem is that Game1 needs the full definition of GameComponent, and that GameComponent needs the full definition of Game1. I had way too much trouble having these in separate headers, so that’s why they’re together. Is there any way I can do this without completely changing the implementation of one of the classes?
Thanks!
Think about the computer’s memory for a second here.
Now the question the compiler needs to answer is How much space should I reserve for
x?Let’s see. The first byte of
xisbyte aa;. Easy enough. That’s 1 byte.Next comes
B ab;. Let’s see what’s in there.The first byte of
x.abis abyte bb;. That’s 2 bytes forxso far.Next, is a
A ba;. Let’s see what’s in there.The first byte of
x.ab.bais abyte aa;. That’s 3 bytes forxso far.And so on and so forth ad infinitum.
How big is
x? The correct answer is of course*** OUT OF CHEESE ERROR ***.The compiler doesn’t actually do this because it knows it can’t handle this case – so the syntax doesn’t allow circular containment in the first place.
Here’s a diagram of the contents of

xin this example:UPDATE
Apparently, I forgot to include a solution here. Now that you understand what the problem is, the solution should be pretty simple. Use pointers. Either use a pointer from
AtoBand letBincludeAas it already does, or vice versa, or use two pointers. Then, you won’t have circular inclusion – ifBdoesn’t include a copy ofAbut just a pointer to it, that fixes the entire issue here.