如何避免头文件重复

How to avoid header file duplication?

本文关键字:文件 何避免      更新时间:2023-10-16

在我的程序中,我得到了两个名为"invader.h"和"game.h"的头文件。在 game.h 中,我包含 invader.h,并且因为我想要将当前游戏实例的指针传递给入侵者实例。我还在入侵者.h中包含game.h,但是我遇到了编译错误。如果我从入侵者.h中删除game.h,它可以正常工作。我已经在每个头文件中添加了包含保护。根据我到目前为止的发现,我在 invader.h 中添加了一个游戏类的前向声明,因为我需要的是指向 invader.h 中游戏实例的指针。但是当我想在 invader.cpp 中调用游戏函数时,它说不允许指向不完整类类型的指针。我应该怎么做才能解决这个问题?

游戏.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

入侵者

#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

入侵者.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() 时,它是游戏类的成员函数,发生错误,指出不允许指向不完整类的指针

实际上简单来说,我

想知道的最基本的事情是:在我的代码中,游戏类有一个入侵者类型的成员变量,那么我如何在入侵者类中调用游戏类的成员函数?li,e 我说如果我在 Game.h 中包含 Invader.h 并在 Invader.h 中包含 Gameh.h,就会发生编译错误。

当我在Invader.h中包含Game.h时,这就是得到的:

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

我应该怎么做才能解决这个问题?
作为第一个理解不完整类型的含义:

什么导致类型不完整?

如果您不能使用类型为不完整类型的转发声明,那么您应该重新访问您的设计,因为那里有问题。

如果您需要更详细的答案,则需要提供源代码。

编辑:
您需要在Invader.cpp中包含Game.h

入侵者.cpp

#include "Invader.h"
#include "Game.h"

我将参考我现在在本地框中的两个文件来解释这一点。

server.hworker.h

服务器.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_ */

工人.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

希望有帮助。