Jay Catt
Jay Catt

Reputation: 71

How to display the enum value for something within a class?

I am currently building a poor version of the game "Battleship" and have to use an array of Enums to display the board. For my header I have created:

enum class PlayerPiece {
  AIRCRAFT,
  BATTLESHIP,
  CRUISER,
  SUBMARINE,
  PATROL,
  EMPTY,
 };

 class Board {
 public:

   PlayerPiece playerBoard[100];
   PlayerPiece enemyBoard[100];

   void reset();
   void display() const;

 };

When I get to my source code, I try displaying the board as numbers. As of right now the board is EMPTY after I run my reset command. But after I want to display the array, I get an error saying "no operator << matches these operands ....", I understand that means I need to overload the << command to display properly, but why doesn't it just display the '5' that was assigned? Isn't that the point of Enums? What I have so far is:

 void Board::reset(){
   for (int i = 0; i < 100; ++i){
      playerBoard[i] = PlayerPiece::EMPTY;
      enemyBoard[i] = PlayerPiece::EMPTY;
   };
 }

 void Board::display() const{
  for (int i = 0; i < 100; ++i){

    cout << playerBoard[i] << endl; // 

   };
 }

I have made other codes where I don't have to overload the << operator to display the number attached with ENUM. Am I missing something? Any help would be appreciated.

Upvotes: 2

Views: 196

Answers (2)

Steve Deng Zishi
Steve Deng Zishi

Reputation: 128

If you remove the class and write enum with Unscoped enumeration

enum PlayerPiece {

  AIRCRAFT,

  BATTLESHIP,

  CRUISER,

  SUBMARINE,

  PATROL,

  EMPTY,

 };

You can print the number you wanted.

The difference between scoped and unscoped(from cplusplus.com):

Before C++11, all enums were just, basically, integers. And you could use them like that. It made it too easy to give bad values to functions expecting a restricted set of values. For example:

1
2
3
4
5
6
7
8
9
10
enum round_mode { round_half_up, round_half_down, round_bankers };

double round( double x, round_mode = round_half_up )
{
   ...
};

    int main()
    {
      double x = round( 2.5, 42 );
}

Edit & Run

It compiles, but it isn't pretty.

With C++11, the compiler now knows all kinds of things about your enums, and doesn't let you blithely mix them with incorrect values.

Essentially, it promotes an enum to a first-class object -- it isn't just an integer.

The other issue is that the name for each enumeration bleeds into the containing scope. So the following would be a name conflict:

1 2 enum color_masks { red = 0xFF0000, green = 0x00FF00, blue = 0x0000FF }; int red = 0xFF0000;

You can't have both the identifier 'red' as an enum value and as an integer variable name in the same scope.

While the example here is contrived, it isn't too far off from things that happen all the time -- and that programmers have to take pains to avoid.

(Some identifier names are common. For example, 'max'. If you #include , there's a 'max' macro in there, which plays havoc if you also #include and try to use the 'max' function, or #include and try to find the numeric_limit ::max(). I know that's a macro problem, but it's the first name conflict I could come up with...)

Upvotes: 0

Carlton
Carlton

Reputation: 4297

If you want to see the number associated with the scoped enum type, use a static_cast like this:

cout << static_cast<int>(playerBoard[i]) << endl;

Normal (unscoped) enums don't need this cast, as their types implicitly cast to int, or whatever underlying type you specified. That's probably why this hasn't happened to you before.

Upvotes: 2

Related Questions