1

I just started to learn c++ and i have the following problem in this simple code:

enum class color_type {green,red,black};

color_type color(color_type::red);

I get the error "color_type is not a class or namespace". My goal is to create a variable of type color_type that can only take values red, black and green. Could you please help me? Thank you

8
  • 3
    I cannot reproduce this error from your sample: ideone.com/WVjvKq ? Commented May 30, 2015 at 10:08
  • 4
    Looks good to me, by chance what compiler/IDE are you using? Commented May 30, 2015 at 10:09
  • 1
    Double check spelling in your actual code. There are no problems with the code you put here :) Commented May 30, 2015 at 10:09
  • May be related: stackoverflow.com/questions/22238391/… Commented May 30, 2015 at 10:27
  • 1
    That's your IDE. The compiler is listed under the toolchain in project options. Commented May 30, 2015 at 10:55

2 Answers 2

1

Your code looks like valid c++11 to me.

If your compiler does not support c++11 then you simulate an enum class with a namespace or struct like so

 struct colour_type
 {
      enum value
      {
            red, 
            green,
            blue
      }
 }

 //usage is like so
 colour_type::value myColour = colour_type::red;

It's not perfect but it keeps the enum in its own scope.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I enabled the support for c++11 and it works now!
0

It seems that your compiler does not support qualified names of unscoped enumerators (I mean your post before its edition when there was shown an unscoped enumeration). Write simply

enum color_type {green,red,back};
color_type color(red);

Or you could use a scoped enumeration as for example

enum class color_type {green,red,back};
color_type color(color_type::red);

In fact these declarations

enum color_type {green,red,back};
color_type color(color_type::red);

are correct according to the current C++ Standard.

3 Comments

I think, c++11 has a require to specify enum class's scope, is not it? It does not compile also on g++ 4.9.2 with c++11 so you may be wrong and I am right :)
@Victor Polevoy You may use qualified names with unscoped enumerators.
Thank you very much. I was using code::blocks and the support for c++11 wasnt enabled. Now it works!

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.