【发布时间】:2018-04-06 03:26:11
【问题描述】:
我正在开发一款 Snake 游戏,但在使用 C++ 循环引用时遇到了问题。 这是 SnakeBody 类的标头,它代表 Snake 的任何非标头部分。
#ifndef SNAKEBODY_HPP_
#define SNAKEBODY_HPP_
class SnakeBody {
public:
SnakeBody(SnakeHead *origin, int left, int x, int y);
~SnakeBody() = default;
void move();
void grow(int size);
protected:
SnakeBody *next;
SnakeHead *head;
SnakeGame *game;
int x;
int y;
int growWait;
};
#endif /* !SNAKEBODY_HPP_ */
还有源代码(包含构造函数的部分)。
#include "../include/SnakeHead.hpp"
#include "../include/SnakeGame.hpp"
#include "../include/SnakeBody.hpp"
SnakeBody::SnakeBody(SnakeHead *origin, int left, int x, int y)
{
this->head = origin;
this->game = this->head->getGame();
this->x = x;
this->y = y;
this->growWait = 0;
if (left > 0)
this->next = new SnakeBody(origin, left - 1, x + 1, y);
else
this->next = nullptr;
}
我一直小心地将每个包含在源代码中而不是标题中,以避免未完成的引用和循环。但似乎 SnakeHead 有一个未定义的引用。
SnakeBody:蛇的任何非头部部分。 SnakeHead :蛇的头部。 SnakeGame : 包含棋盘状态,需要 SnakeHead 和 SnakeHead 的引用,SnakeBody 需要 SnakeGame 的引用来获取有关游戏状态的数据(即:墙、食物等... )。
错误信息是:./src/../include/SnakeBody.hpp:19:9: error: ‘SnakeHead’没有命名类型;你的意思是“SnakeBody”吗? 蛇头 *head;
【问题讨论】:
-
您确定未定义的引用吗?未定义的引用通常意味着标头很好,但在某处缺少函数或变量定义。
-
我们能得到完整的错误信息吗?
-
我已经添加了错误信息
-
您需要在
SnakeBody.hpp中转发声明SnakeHead
标签: c++ compilation header