包含标题的枚举| 错误未声明

Piu*_*sch 1 c++ enums declaration

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_ */
Run Code Online (Sandbox Code Playgroud)

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_ */
Run Code Online (Sandbox Code Playgroud)

我不知道为什么没有声明枚举game_status.我已正确包含标题,它在范围内,不是吗?我无法在"Player.h"中声明枚举.我会宣布它两次.你能帮我吗?你有什么建议吗?

(我不允许使用C++ 11)

提前致谢

Luc*_*ore 7

问题是循环包含,删除

#include "Player.h"
Run Code Online (Sandbox Code Playgroud)

来自Maumau_game.h它应该工作.只包含您需要的内容并向前声明您可以做的任何事情.


Rax*_*van 5

问题是您Player.h包含Maumau_game.h了尝试包含的包含Player.h,而最后包含的定义不存在。

解决这个问题的一种方法是在Player.h(通过添加一行enum game_status)前向声明枚举,删除包含 for Maumau_game.h(from Player.h)并将参数从函数更改game_status g_statusconst game_status& g_statusin do_turn

  • IIRC 前向声明枚举不是标准的。@L1ttleb1rd 正确的解决方法是删除多余的包含,在编译之前不要移动定义。见 http://stackoverflow.com/a/72599/673730 (2认同)