为什么我得到这个“枚举”不是类或命名空间错误? [英] Why am I getting this 'enum' is not a class or a namespace error?

查看:230
本文介绍了为什么我得到这个“枚举”不是类或命名空间错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  void createPlayer(Player& player,PlayerType& playerType ); 

我有一个像这样定义的玩家:

  using namespace std; 

枚举PlayerType {FORWARD,DEFENSEMAN,GOALIE};

class Player {
public:
Player();
void setType(PlayerType);
private:
PlayerType类型;
};

这是我如何在main ...中调用方法...

  #includePlayer.h
#includeManager.h

int main(){$ b经理经理

$ b玩家玩家;
PlayerType t = PlayerType :: FORWARD;
manager.createPlayer(player,t);

return 0;
}

...但是无法使用此错误进行编译:

  Main.cc:在函数'int main()'中:
Main.cc:12:18:error:'PlayerType'不是一个类或命名空间

任何想法?注意:我无法更改createPlayer方法的签名。

解决方案

枚举不创建命名空间。



因此 PlayerType t = PlayerType :: FORWARD; 应更改为:

  PlayerType t = FORWARD; 






请注意,c ++ 11介绍枚举类 es,它们有一个命名空间。除此之外,MSVC还有一个扩展名,可以处理(常规)枚举,就像他们有命名空间一样。所以你的代码实际上可以用MSVC。


I need call a method with this signature in my Manager class:

void createPlayer(Player& player, PlayerType& playerType);

I have a Player defined like so:

using namespace std;

enum PlayerType { FORWARD, DEFENSEMAN, GOALIE };

class Player {
  public:
    Player();
    void setType(PlayerType);
  private:
    PlayerType type;
};

This is how I try to call the method in main ...

#include "Player.h"
#include "Manager.h"

int main() {

  Manager manager;
  Player player;
  PlayerType t = PlayerType::FORWARD;
  manager.createPlayer(player, t);

  return 0;
}

... but it fails to compile with this error:

Main.cc: In function ‘int main()’:
Main.cc:12:18: error: ‘PlayerType’ is not a class or namespace

Any ideas? Note: I cannot change the signature of the createPlayer method.

解决方案

enum doesn´t create a namespace.

Therefor PlayerType t = PlayerType::FORWARD; should be changed to:

PlayerType t = FORWARD;


Notice that c++11 introduce enum classes, which have a namespace. Beside this MSVC has an extension which treats (regular) enums like they have namespace. So your code should actually work with MSVC.

这篇关于为什么我得到这个“枚举”不是类或命名空间错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆