Thread: scoped enums only available with -std=c++0x or std=gnu++0x

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Bozeat, Northants. UK
    Posts
    23

    scoped enums only available with -std=c++0x or std=gnu++0x

    Hi - First post, so please be gentle with me if I breached any rules or anything... Thanks.

    I am reading C++ Without Fear (second edition). Near the end of chapter 10 I am tasked with updating a "rock Paper scissors" game.

    Now, I understand the code and I am pretty sure that I can complete the tasks set, however, when I bring the example program into Dev-C++ OR Code:Blocks I am unable to compile it. Here's the first part of the code - The GCC compiler stops at the "enum" line:

    Code:
    #include <iostream>
    #include <string>
    #include <ctime>
    using namespace std;
    
    
    enum class Choice { rock, paper, scissors };
    Dev-C++ says:
    expected identifier before "class".

    This is very frustrating as this is the IDE recommended by the book... and the compiler came bundled with it.

    As a test I tried Codeblocks and codeblocks tells me:
    scoped enums only available with -std=c++0x or std=gnu++0x

    Am I right in thinking that I have out of date compiler that doesn't understand C++0x instructions? and if so, can someone point me in the right direction to get one please?

    Thanks in advance,

    B.

    (I hope this is in the right section of the forum, if not please accept my apologies.)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Brian_of_Bozeat
    Am I right in thinking that I have out of date compiler that doesn't understand C++0x instructions?
    Yes. That said, the new C++ standard was only published this year

    Quote Originally Posted by Brian_of_Bozeat
    can someone point me in the right direction to get one please?
    Since your version of CodeBlocks comes with a compiler that does support this new feature, just configure the compiler (e.g., through CodeBlocks) to enable it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Bozeat, Northants. UK
    Posts
    23
    Thanks for your help, I ticked the box in the global compiler settings in codeblocks that said:


    Have G++ Follow the coming C++ ISO C++ language standard [-std=c++0x]

    Is that all I needed to do?

    The reason I ask is because I am now stuck at the line of code that declares using namespace choice;


    Code:
    #include <iostream>
    #include <string>
    #include <ctime>
    using namespace std;
    
    
    enum class Choice { rock, paper, scissors };
    using namespace Choice;
    
    
    Choice player_choice;     // Holds user's move
    Choice computer_choice;   // Holds computer move

    The errors (from codeblocks) are:


    'choice' is not a namespace-name
    and
    expected namespace-name before ";" token

    Surely this is not a C++0x problem... This is very frustrating, (I'm a bit angry with the author over this, how can anyone learn from code that doesn't work?)

    Anyway,like I said, thanks V much, I'm grateful for your help. The next chapter in the book is about classes so perhaps I will learn a bit more and come back to this one.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Well.. Choice isn't a namespace...It is just a scope for the enum values.
    You'd still have to put Choice::rock ..etc when using the values.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Bozeat, Northants. UK
    Posts
    23
    Thanks for taking the time to help me,

    Quote from Book:

    C++0x supports both weak and strong enum (enumerated types). Use of enum
    class (see the following example) creates a strongly typed set of enumerated values
    in which a separate namespace is created and values cannot be assigned to or
    from another integer type without a cast.
    Code:
    enum class type_name { symbols };
    When using this version (strongly typed), remember the symbols listed are in a
    separate namespace and have to be referred to with class scope, unless you have a
    using namespace statement. For example:
    Code:
    enum class Choice { rock, paper, scissors };
    
    
    Choice player = Choice::rock;
    
    
    using namespace Choice;
    Choice comp = scissors; // Now this is ok.

    So, is the book in error? - it would be a shame as this book is recommended on this site as the first step on the path from beginner to expert. I'm hoping it's another setting in my compiler that I need to set...

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I'm not sure... but it appears to be an error in the book..(Anyone willing to verify in in the Standard Doc ? I'm afraid of reading them!)
    Anyway.. I get an error for
    Code:
    enum class foo{a,b,c};
    using namespace foo; // Same for "using foo::a;"
    int main()
    {
        foo x = a;
        return 0;
    }
    [manasij7479@manasijn ~]$ g++ a.cpp -std=c++0x
    a.cpp:3:17: error: ‘foo’ is not a namespace-name
    a.cpp:3:20: error: expected namespace-name before ‘;’ token
    a.cpp: In function ‘int main()’:
    a.cpp:6:13: error: ‘a’ was not declared in this scope
    But not for
    Code:
    enum class foo{a,b,c};
    int main()
    {
        foo x = foo::a;
        return 0;
    }
    P.S : It could also be a bug in the compiler. But it is unlikely..as my version's C++11 support for strong enums is marked as complete.
    Last edited by manasij7479; 12-01-2011 at 09:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scoped lock and object life time
    By pheres in forum C++ Programming
    Replies: 0
    Last Post: 01-22-2008, 02:25 AM
  2. Enums and Templates
    By einarp in forum C++ Programming
    Replies: 5
    Last Post: 07-19-2006, 05:07 AM
  3. printing enums
    By xddxogm3 in forum C++ Programming
    Replies: 19
    Last Post: 08-09-2005, 12:08 PM
  4. C/C++-: dynamically scoped language
    By silicon in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2005, 06:55 PM
  5. enums!
    By newjamie in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 12:25 PM