如何避免头文件重复
How to avoid header file duplication?
在我的程序中,我得到了两个名为"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.h
和worker.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
希望有帮助。
- 避免使用 boost::进程间::消息队列创建文件
- 在多个头文件中从接口声明被覆盖的函数时,如何避免重复代码?
- 避免遗漏包含相同文件的最佳方法是什么?
- 当 PHP、C++ 和 shell 脚本尝试访问同一个文件时,如何避免错误?
- 连接两个文件时如何避免冗余尾随换行符?
- Cmake生成了VS项目的循环依赖关系,但没有制作文件.如何避免
- 从文件的一行中查找数字的平均值,避免使用第一个单词
- 将C 数组写成文件,避免创建std :: String
- 在C 中读取文本文件时避免使用空间和行
- 是否有一种方法可以避免标头文件中使用的constexpr函数输入全局范围,而无需额外的名称空间
- Matlab 如何知道有一个 .mex64 文件并避免无穷大"compiling"循环
- 如何避免在头文件中定义整个模板类
- 如何修复生成文件以正确包含 -lcrypto 以避免链接器错误:未定义对"EVP_CIPHER_CTX_new"的引用
- 锁定文件,避免使用相同的过程以访问两次
- 如何在不同的类中使用相同的头文件以避免"use of undefined type"
- 是否有方法将相对库路径添加到可执行文件以避免设置LD_library_path
- 如果两个类必须相互包含,我应该如何排列它们的头文件并避免不完整的类型
- 如何在读取文件时避免atoi
- 避免在 gperf 输出文件中避免"警告:声明 UserSuppliedStruct 不声明任何内容"
- 读取文件时避免错误标志