1

Sleepy programmer (self - acclaimed) need help

#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

Above is header file called game.h

Whenever I try to compile the project it shows error saying "error: 'food' does not name a type"

where food is another class which I have included in this header file too

#include 'food.h'

but still the error persists. I also, tried include the food.h in .cpp file of the game but still no hope.

this is the food.h file in case you need it

#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

I am using Code::blocks.

Things I have done

  1. Reparse the project

  2. Rebuild the entire project

  3. Rewriting the class again

  4. Restarting the laptop

  5. Restarting Code::blocks

  6. Provided the path in the compiler settings -> search directories

but none worked. I don't know what is the problem ?

Thanks in advance

Anim Malvat
  • 32
  • 1
  • 6

1 Answers1

1

You have a circular dependency. Food includes game and game includes food.

Since you have correctly used header guards, instead of infinitely including each other endlessly one is simply included before the other. It so happened that the definition of game was included first, so it doesn't know that food is a type.

Solution: Do not have circular dependencies. It appears that food class doesn't use anything from game, so this should be easy to achieve.

but there are some macros in game.h

Then split your header into smaller pieces so that food no longer depends on any header that depends on food.

eerorika
  • 232,697
  • 12
  • 197
  • 326