1

Maumau_game.h:

#ifndef MAUMAU_GAME_H_
#define MAUMAU_GAME_H_

#include <more_includes>
#include "Player.h"

enum game_status {
    STATUS_NONE = 0,
    STATUS_DRAW_2 = 1,
};

class Maumaugame_holder {
methods();
};

#endif /* MAUMAU_GAME_H_ */

Player.h:

#ifndef PLAYER_H_
#define PLAYER_H_

#include <vector>
#include "Maumau_game.h"
#include "Card.h"

class Player {
more_methods();

public:
    void do_turn(game_status g_status, const Card upper_dropped_card,
            Deck& talon); //Error: >>game_status<< not declared 


};

#endif /* PLAYER_H_ */

I have no clue why the enum game_status is not declared. I have included the header properly and it is in the scope, isn't it? I cant declare the enum in the "Player.h" eiter. I would have declared it twice. Can you help me please? Do you have any suggestions?

(I am not allowed to use C++11)

Thanks in advance

Pius Friesch
  • 351
  • 1
  • 6
  • 14
  • 1
    You have a circular include. Do not include player.h inside Maumau_game.h – drescherjm Dec 11 '13 at 14:07
  • Possible duplicate: http://stackoverflow.com/questions/1909825/error-with-the-declaration-of-enum . At any rate I think the answers in this question will fix your problem. – Paul O. Dec 11 '13 at 14:09
  • This isn't the problem, but those initializers in the `enum` are redundant. – Pete Becker Dec 11 '13 at 14:47

2 Answers2

7

The problem is a circular include, remove the

#include "Player.h"

from Maumau_game.h and it should work. Only include what you need and forward-declare anything you can.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
5

The problem is that you include Player.h that includes Maumau_game.h that tries to include Player.h and at that last include the definition is not present.

One way to fix this is to forward declare the enum in Player.h (by adding a line enum game_status) , remove the include for Maumau_game.h (from Player.h) and change the argument from game_status g_status to const game_status& g_status in do_turn function.

Raxvan
  • 6,257
  • 2
  • 25
  • 46
  • "and so on until the compiler will stops reasoning" - not really, they're each included at most twice because of the include guards. The problem is the order in which the types are declared. – Luchian Grigore Dec 11 '13 at 14:27
  • 1
    @Luchian Grigore whell yes , i just wanted to make it sound more dramathic :) – Raxvan Dec 11 '13 at 14:30
  • I moved the enum "game_status" to Player.h and removed the circular include. It worked. But is there any way to declare the enum in "Maumaugame_holder.h" and use it in the Player Class? – Pius Friesch Dec 11 '13 at 14:34
  • @L1ttleb1rd not directly, i mean that you have to forward declare the enum in the header (this tell the compiler about it's existence) and after that use it in the cpp where you can include the acutal definition of the enum. To forward declare you can write `enum game_status;` in the `Maumaugame_holder.h` and define it in the `player.h` After that you can use it in the Maumaugame_holder.cpp where you can safely include `Player.h`. Search on google for `c++ forward declaration` for further information bout this. – Raxvan Dec 11 '13 at 14:35
  • 2
    IIRC forward-declaring enum's isn't standard. @L1ttleb1rd the correct fix would be to remove superfluous includes, not moving definitions around until it compiles. See http://stackoverflow.com/a/72599/673730 – Luchian Grigore Dec 11 '13 at 14:38
  • @Luchian Grigore forwarding enum declaration is indeed strange, but you can use reference in any declaration so it's ok. The best way is to move the enum declaration into a separate header and include that everywhere. – Raxvan Dec 11 '13 at 14:42
  • Not strange, but illegal (but there are compiler extensions that allow it). It shouldn't be done. – Luchian Grigore Dec 11 '13 at 14:46
  • I am only allowed to use c++98, so forward declaration of the enum is not an option. @LuchianGrigore What is your opinion about outsourcing the enum (or all enums) in a single header file and include this in both code files? – Pius Friesch Dec 11 '13 at 14:49