【发布时间】:2013-09-26 19:23:01
【问题描述】:
例如一个包含三个类 Player、Bot 和 Game 的小电脑游戏
玩家有一个方法来检查玩家是否与机器人发生碰撞
// Player.h
#include Game.h
#include Bot.h
class Player {
private:
bool collision(Game g) {
for (Bot bot: g.bots)
...
}
};
Bot.h(保持简单,因为它还有一些其他属性,例如实际位置和到目前为止)
// Bot.h
class Bot {
public:
Bot()
};
Gameclass 处理 Gameloop 和 Bot 列表
//Game.h
#include Bot.h
#include Player.h
class Game {
public:
Player player:
std::vector<Bot> bots
void loop() { player.collision() }
};
所以这里我们遇到了 Game.h 包含 Player.h 的问题,反之亦然。
我该如何解决这个问题?
【问题讨论】:
-
为什么 player.h 包含 game.h?在您发布的代码中没有理由。如果 player.h 有充分的理由包含 game.h,那么我们需要知道它是什么,然后才能给出解决方案。
-
约翰,你是对的,谢谢。我已经纠正了。 for 循环应该在玩家类中,而不是在游戏中。
标签: c++