Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > scoped enum issue with MS VS C++

Reply
Thread Tools

scoped enum issue with MS VS C++

 
 
bobl0456@gmail.com
Guest
Posts: n/a
 
      10-21-2013
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


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-21-2013
On 21.10.2013 20:37, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
kfrank29.c@gmail.com
Guest
Posts: n/a
 
      10-21-2013
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, (E-Mail Removed) 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
 
Reply With Quote
 
bobl0456@gmail.com
Guest
Posts: n/a
 
      10-21-2013
Thanks for the responses.

Bob

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scoped enum question woodbrian77@gmail.com C++ 21 06-13-2013 04:49 PM
Help with printing a bit pattern with printf and %x matt.jaffe@gmail.com C Programming 9 04-19-2013 12:14 PM
Possible to promote scoped enum values to enclosing namespace? Old Wolf C++ 5 02-08-2013 04:05 PM
scoped enum implementation Jonathan Mcdougall C++ 0 02-13-2005 05:50 AM
JBoss: Scoped beans & Remote classloader issue Cruinne Java 0 07-22-2004 12:07 PM



Advertisments