我需要在我的 Manager 类中调用一个带有此签名的方法:
void createPlayer(Player& player, PlayerType& playerType);
我有一个这样定义的玩家:
using namespace std;
enum PlayerType { FORWARD, DEFENSEMAN, GOALIE };
class Player {
public:
Player();
void setType(PlayerType);
private:
PlayerType type;
};
这就是我尝试调用 main 中的方法的方式 ...
#include "Player.h"
#include "Manager.h"
int main() {
Manager manager;
Player player;
PlayerType t = PlayerType::FORWARD;
manager.createPlayer(player, t);
return 0;
}
...但编译失败并出现此错误:
Main.cc: In function ‘int main()’:
Main.cc:12:18: error: ‘PlayerType’ is not a class or namespace
有什么想法吗?注意:我无法更改 createPlayer 方法的签名。
最佳答案
enum
不创建命名空间。
因此 PlayerType t = PlayerType::FORWARD;
应更改为:
PlayerType t = FORWARD;
请注意,c++11 引入了具有命名空间的enum class
es。除了这个 MSVC 有一个扩展,它对待(常规)枚举就像它们有命名空间一样。所以你的代码实际上应该与 MSVC 一起工作。
关于c++ - 为什么我会收到此 'enum' 不是类或命名空间错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22238391/