How to avoid duplicate header?

Hi guys, in my program I have two header files named "invader.h" and "game.h", in game.h I turn on invader.h and because I want to pass a pointer to the current instance of the game on an instance of invader . I also include game.h in invader.h, but I got a compilation of error.if I remove game.h from invader.h, it works fine. I am already adding an include guard in every header file. based on what I have found so far, I add a forward declaration of the game class in invader.h, because what I need is a pointer to a game instance in invader.h, but when I want to call the game function in invader. cpp, it says that a pointer to an incomplete type of a class is invalid. What should I do to solve this problem?

Game.h

#ifndef GAME_H
#define GAME_H
#include "Tank.h"
#include "Invader.h"
#include "Block.h"
#include "Bullet.h"

class Game
{
private:
Tank tank;
Invader invaders[11][5];
Block blocks[4];
bool logicRequiredThisLoop = false;
public:
Game();
void initEntities();
Tank* getTank(){return &tank;};
Invader* getInvaders(){return &invaders[0][0];};
Block* getBlocks(){return &blocks[0];};
void updateLogic();
};
#endif

Invader.h

#ifndef INVADER_H
#define INVADER_H
#include "Entity.h"
class Game; //forward declaration of class Game
class Invader: public Entity
{
private:
Game* game;
public:
Invader(){};
Invader(Game*,char*,int,int,int,int,int,int);
void move(long delta);
void doLogic();
};
#endif

Invader.cpp

#include "Invader.h"
Invader::Invader(Game* game,char* sprite,int x,int y,int dx,int dy,int width,int height):Entity(sprite,x,y,dx,dy,width,height)
{
this->game = game;
}

void Invader::move(long delta)
{
if ((dx<0)&&(x<=10))
{
    game->updateLogic();
}
if ((dx>0)&&(x>=390))
{
    dx = -dx;
    y -= dy;
}

x+=dx;
}

Invader.cpp, updateLogic(), - Game, , ,

, , : Game class - invader, - Game invader? li, e , Invader.h Game.h Gameh.h Invader.h .

, Game.h Invader.h:

1>ClCompile:
1>  Invader.cpp
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(13): error C2146: syntax error : missing ';' before identifier 'invaders'
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): error C2143: syntax error : missing ';' before '*'
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): warning C4183: 'getInvaders': missing return type; assumed to be a member function returning 'int'
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): error C2065: 'invaders' : undeclared identifier
+3
2

, ?
, :

?

" ", , - .

, .

EDIT:
Game.h Invader.cpp.

//Invader.cpp

#include "Invader.h"
#include "Game.h"
+1

, .

server.h worker.h

server.h:

#ifndef SERVER_H_
#define SERVER_H_

typedef struct _conf conf;
typedef struct _worker worker;
typedef struct _logger logger;

typedef struct _server server;
struct _server {
    /* config */
    conf *cfg;

    /* socket */
    int fd;
    struct event_base *base;
    struct event *signal;

    /* workers */
    worker **w;

    /* log */
    logger *log;
};
...
...
#endif /* SERVER_H_ */

worker.h

#ifndef WORKER_H_
#define WORKER_H_

#include <pthread.h>

typedef struct _server server;

typedef struct _worker worker;
struct _worker {
    pthread_t t;

    struct event_base *base;
    struct evhttp *http;

    server *s;
};
...
...
#endif /* WORKER_H_ */

, , , .h: ,

typedef struct _worker worker; 

server.h , . : https://github.com/abhinavsingh/pulsar/tree/master/include

, .

0

All Articles