c++ - 为什么我会收到此 'enum' 不是类或命名空间错误?

标签 c++ enums

我需要在我的 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 classes。除了这个 MSVC 有一个扩展,它对待(常规)枚举就像它们有命名空间一样。所以你的代码实际上应该与 MSVC 一起工作。

关于c++ - 为什么我会收到此 'enum' 不是类或命名空间错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22238391/

相关文章:

c++ - 避免转换容器的元素指针类型

c# - 使用 ValueInjecter 在枚举之间映射

java - java - 如何通过覆盖方法来使用java枚举中的字段?

ios - 没有用于分配给属性的 setter 方法

c++ - NULL 和 nullptr 比较

c++ - 带有 C++ 方法的 gcc 属性

c++ - 如何在 C++ 中使用枚举

java - 是否可以在 Java 枚举中使用上层范围对象?

python - zeromq (zmq) 缺少带有 c++ 发布者和 python 订阅者的消息

c++ - 调试堆栈损坏方法