Error: 'some class' does not name a type. how to resolve this?

Error: 'some class' does not name a type. how to resolve this?

昏昏欲睡的程序员(自称)需要帮助

#ifndef GAME_H
#define GAME_H
#include <iostream>
#include "snake.h"
#include "food.h"
#define MAX_WIDTH 20
#define MAX_HEIGHT 20

class game
{
    public:
        game();
        static void createBoard();
        int gameStart();
        bool gameChecker();

    protected:
        food apple;
        bool gameOver = false;
        snake python;
        int gameSpeed = 250;

    private:
};

#endif // GAME_H

上面是名为 game.h

的头文件

每当我尝试编译项目时,它都会显示错误消息“错误:'food' 没有命名类型

其中 food 是另一个 class,我也将其包含在此头文件中

#include 'food.h'

但错误仍然存​​在。我也试过在游戏的 .cpp 文件中包含 food.h 但仍然没有希望。

这是 food.h 文件,以备不时之需

#ifndef FOOD_H
#define FOOD_H
#include "game.h"
#include "snakeFragment.h"
#include <stdlib.h>
#include <time.h>

class food: public snakeFragment
{
    public:
        food();
        int foodPlace();
        int printFood();

    protected:
        char symbol = '*';

    private:
};

#endif // FOOD_H

我正在使用 Code::blocks。

我做过的事情

  1. 重新分析项目

  2. 重建整个项目

  3. 再次重写class

  4. 正在重启笔记本电脑

  5. 正在重启Code::blocks

  6. 在编译器设置->搜索目录中提供路径

但 none 有效。不知道是什么问题?

提前致谢

你有循环依赖。食物包括游戏,游戏包括食物。

因为你已经正确地使用了 header 个守卫,而不是无限地相互包含,一个简单地包含在另一个之前。正好game的定义先包含进去了,所以不知道food是一个type

解决方法:不要有循环依赖。看来食物 class 没有使用游戏中的任何东西,所以这应该很容易实现。

but there are some macros in game.h

然后将你的 header 分成更小的部分,这样食物就不再依赖任何依赖食物的 header。