gpt4 book ai didi

c++ - 如何避免头文件重复?

转载 作者:太空狗 更新时间:2023-10-29 23:08:18 27 4
gpt4 key购买 nike

在我的程序中,我有两个名为“invader.h”和“game.h”的头文件。在 game.h 中,我包含了 invader.h,因为我想将当前游戏实例的指针传递给入侵者实例。我还在 invader.h 中包含了 game.h,但出现了编译错误。如果我从 invader.h 中删除 game.h,它工作正常。我已经在每个头文件中添加了 include guard。根据我目前的发现,我在 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

入侵者.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

入侵者.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 中,当我尝试调用 Game 类的成员函数 updateLogic() 时,发生错误,指出不允许指向不完整类的指针

其实简单来说我想知道的最基本的事情是:在我的代码中Game类有一个invader类型的成员变量,那么我如何在invader类中调用Game类的成员函数?li,e我说如果我在 Game.h 中包含 Invader.h,在 Invader.h 中包含 Gameh.h,出现编译错误。

这是我在 Invader.h 中包含 Game.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

最佳答案

我应该怎么做才能解决这个问题?
首先了解不完整类型的含义:

What leads to incomplete types?

如果您不能在类型不完整的情况下使用前向声明,那么您应该重新访问您的设计,因为那里有问题。

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

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

//入侵者.cpp

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

关于c++ - 如何避免头文件重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10220886/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com