Error C2079, what is the order in which classes are defined in

I get 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 a full definition of GameComponent, and that GameComponent needs a full definition of Game1. I had too many problems with these individual headers, so why are they together. Is there a way to do this without completely changing the implementation of one of the classes?

Thank!

+4
source share
3 answers

Think a second about computer memory.

class B;

class A {
    byte aa;
    B ab;
};

class B {
    byte bb;
    A ba;
};

A x;

Now the question the compiler should answer is: how much space should I reserve for x?

. x - byte aa;. . 1 .
B ab;. , .
x.ab - byte bb;. 2 x.
A ba;. , .
x.ab.ba - byte aa;. 3 x.
, .
x? , , *** OUT OF CHEESE ERROR ***.

, , - .


x :
Diagram


UPDATE

, . , , , . . A B, B A, , , . - B A, , .

+11

, , , , .

:


class A;
class B;
class A {
   B b;
};

class B {
   A a;
};

. . A B, A .. .

:


class A;
class B;
class A {
   B *b;
};

class B {
   A *a;
};

. - , , . , .

+4

Due to the risk of being off topic: you can do this in C #, but if the nested members are not loaded on demand, you will immediately crash on the first instance.

+1
source

Source: https://habr.com/ru/post/1766596/


All Articles