Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

c++ - Why am I getting this 'enum' is not a class or a namespace error?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...