scoped enum issue with MS VS C++

B

bobl0456

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
 
A

Alf P. Steinbach

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. :(
 
K

kfrank29.c

Hi Bob!

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

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.


Happy C++11 Hacking!


K. Frank
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top