scoped enum issue with MS VS C++

Discussion in 'C++' started by bobl0456@gmail.com, Oct 21, 2013.

  1. Guest

    I get the following error:

    TurtleGraphics.cpp(93): error C2677: binary '!=' : no global operator found which takes type 'Cmds' (or there is no acceptable conversion)

    for the following statement:

    } while ( cmdsArray[cmdNo] != Cmds::END_OF_DATA ); // repeat until end of data reached

    Cmds is defined as:

    enum class Cmds { PEN_UP = 1, PEN_DWN, TURN_RIGHT, TURN_LEFT, MOVE, DISPLAY, END_OF_DATA = 9};

    and cmdsArray is a pointer based array of type int.

    I do not understand why this should not work. BTW, I know that if I change the enum back to traditional unscoped enum it will work fine.

    Bob
     
    , Oct 21, 2013
    #1
    1. Advertising

  2. On 21.10.2013 20:37, wrote:
    > I get the following error:
    >
    > TurtleGraphics.cpp(93): error C2677: binary '!=' : no global operator found which takes type 'Cmds' (or there is no acceptable conversion)
    >
    > for the following statement:
    >
    > } while ( cmdsArray[cmdNo] != Cmds::END_OF_DATA ); // repeat until end of data reached
    >
    > Cmds is defined as:
    >
    > enum class Cmds { PEN_UP = 1, PEN_DWN, TURN_RIGHT, TURN_LEFT, MOVE, DISPLAY, END_OF_DATA = 9};
    >
    > and cmdsArray is a pointer based array of type int.
    >
    > I do not understand why this should not work.


    Using the word "class" you asked for a bit stronger type checking.

    You got it.


    > BTW, I know that if I change the enum back to traditional unscoped enum it will work fine.


    Instead, fix the array type.


    Cheers & hth.,

    - Alf

    PS: Using ALL UPPERCASE for constants is a Java convention. In C++ ALL
    UPPERCASE is conventionally reserved for macro names, in order to reduce
    the chance of name collisions and unintended text substitution. Java
    doesn't have macros. Using such names for mere constants, as in Java, in
    C++ increases the chance of name collisions and unintended text
    substitution, i.e. it's /directly in conflict/ with the common C++
    convention. It's also an all out visual attack, hurting the eyes. And
    ears. :(
     
    Alf P. Steinbach, Oct 21, 2013
    #2
    1. Advertising

  3. Guest

    Hi Bob!

    I can't comment specifically on the microsoft compiler,
    but it appears to be acting according to the standard.

    On Monday, October 21, 2013 2:37:06 PM UTC-4, wrote:
    > I get the following error:
    >
    > TurtleGraphics.cpp(93): error C2677: binary '!=' : no global operator found which takes type 'Cmds' (or there is no acceptable conversion)
    >
    > for the following statement:
    >
    > } while ( cmdsArray[cmdNo] != Cmds::END_OF_DATA ); // repeat until end of data reached
    >
    > Cmds is defined as:
    >
    > enum class Cmds { PEN_UP = 1, PEN_DWN, TURN_RIGHT, TURN_LEFT, MOVE, DISPLAY, END_OF_DATA = 9};
    >
    > and cmdsArray is a pointer based array of type int.
    >
    > I do not understand why this should not work. BTW, I know that if I change the enum back to traditional unscoped enum it will work fine.


    The key issue is that enum classes (scoped enums)
    in c++11 do not implicitly convert to ints (unlike
    "traditional" enums).

    Let me quote from Stroustrup's (very useful) c++11 faq:

    http://www.stroustrup.com/C 11FAQ.html#enum

    The enum classes ("new enums", "strong enums") address
    three problems with traditional C++ enumerations:

    conventional enums implicitly convert to int, causing errors
    when someone does not want an enumeration to act as an integer.

    So, this is a feature (that I almost agree with), not
    a bug.

    Also not the further comment in the faq:

    In the standard library, enum classes are used
    ...
    Several of these have operators, such as == defined.

    Reading between the lines a little here you can make
    it possible to test your enum class against an int for
    equality or inequality, but to do so you have to provide
    your own operator==() and/or operator!=().

    (But following Alf's suggestion, probably the most direct
    fix to your current issue is to make cmdsArray an array
    of Cmds rather than int.

    > Bob



    Happy C++11 Hacking!


    K. Frank
     
    , Oct 21, 2013
    #3
  4. Guest

    Thanks for the responses.

    Bob
     
    , Oct 21, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cruinne
    Replies:
    0
    Views:
    373
    Cruinne
    Jul 22, 2004
  2. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    548
  3. Jonathan Mcdougall

    scoped enum implementation

    Jonathan Mcdougall, Feb 13, 2005, in forum: C++
    Replies:
    0
    Views:
    512
    Jonathan Mcdougall
    Feb 13, 2005
  4. Old Wolf
    Replies:
    5
    Views:
    226
    Martin Shobe
    Feb 8, 2013
  5. Replies:
    21
    Views:
    556
    Luca Risolia
    Jun 13, 2013
Loading...

Share This Page