1

So I was trying to learn Enum classes. I copied this piece of code from my book:

#include <iostream>
using namespace std;
int main()
{
  enum class Color
  {
    RED,
    BLUE
  };

  Color color = Color::RED;

  if (color == Color::RED) 
    cout << "The color is red!\n";
  else if (color == Color::BLUE)
    cout << "The color is blue!\n";

  return 0;
}

I expect the code to print out "The Color is red!" However, my compiler gives this error:

warning: scoped enums only available with -std=c++11 or -std=gnu+11

and

error:'Color' is not a class or namespace

I'm currently using Dev-C++5.11. Any idea how to fix this?

6
  • Please read about MCVEs in the help center. Commented Apr 3, 2016 at 2:05
  • What is "Dev-C++11"? Commented Apr 3, 2016 at 3:44
  • Dev-C++ is a free IDE for Windows that uses either MinGW or TDM-GCC as underlying compiler. Commented Apr 3, 2016 at 3:52
  • sorry can't be more helpful than that I'm new to this. Commented Apr 3, 2016 at 3:53
  • 3
    How about adding -std=c++11 or -std=gnu+11 to the compiler arguments? Commented Apr 3, 2016 at 3:56

3 Answers 3

1

Follow the instructions here to enable C++11 support.

  1. Navigate to Tools -> Compiler Options
  2. Settings tab
  3. Code Generation tab
  4. Change Language standard -std to C++11
Sign up to request clarification or add additional context in comments.

1 Comment

Also, consider trying out a modern IDE such as Visual Studio. ;)
1

Enum classes are a type of enums added to C++ in 2011. For this reason, you have to say to the compiler that you want to use that version of C++ (-std=c++11). Prior versions of C++, like C++03, hadn't that feature.

Dev-C++ uses internally gcc as compiler (g++ and gcc are synonyms), and probably the internal gcc version used by your Dev-C++ version is gcc 4.8.4 or even older, and at that times, the "default" C++ version was C++03 (03 means 2003). So, you have to inform gcc that your source code is written in C++11 with -std=c++11.

Modern versions of gcc or any other compiler assume by default that you are compiling C++11 code (or even, C++14 code), so, you can add that option to your old compiler or update your IDE to a version using C++11 as default.

Comments

0

In C++ 5.1enumerators in the same scope must all be distinct from each other and from other variable names.For example :

enum Color1 {
Bronze,
Silver,
Gold
};

enum Color2
{
Silver, //conflicts with Color1’s Silver
Gold,   //conflicts with Color1’s Gold 
};

C++ 11 solves the scoping problem with a new category called scoped enums.Keyword class appears between the keyword enum and the enum name.You are using C++ 5.1.Hence edit your code as follows:

#include <iostream>
using namespace std;
int main()
{
  enum Color
  {
    RED,
    BLUE
  };

  Color color =RED;

  if (color == RED) 
      cout << "The color is red!\n";
  else if (color == BLUE)
      cout << "The color is blue!\n";
  return 0;
}

https://ideone.com/v42Ihs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.