I noticed that this compiles fine with Visual Studio 2012, but not with Visual Studio 2010? 2010 seems to have an issue with using "class" in the enum declaration.
Most of the problems seem to lie in line 23:
enum class Color : char {Red, Orange, Yellow, Green, Blue, Indigo, Violet};
Code:
// Ex2_09.cpp
// Demonstrating type-safe and non-type-safe enumerations
#include <iostream>
using std::cout;
using std::endl;
// You can define enumerations at global scope
//enum Jewels {Diamonds, Emeralds, Rubies}; // Uncomment this for an error
enum Suit : long {Clubs, Diamonds, Hearts, Spades};
int main()
{
// Using the old enumeration type...
Suit suit = Clubs; // You can use enumerator names directly
Suit another = Suit::Diamonds; // or you can qualify them
// Automatic conversion from enumeration type to integer
cout << "suit value: " << suit << endl;
cout << "Add 10 to another: " << another + 10 << endl;
// Using type-safe enumerations
enum class Color : char {Red, Orange, Yellow, Green, Blue, Indigo, Violet};
Color skyColor(Color::Blue); // You must qualify enumerator names
// Color grassColor(Green); // Uncomment for an error
// No auto conversion to numeric type
cout << endl
<< "Sky color value: "
<< static_cast<long>(skyColor) << endl;
//cout << skyColor + 10L << endl; // Uncomment for an error
cout << "Incremented sky color: "
<< static_cast<long>(skyColor) + 10L // OK with explicit cast
<< endl;
return 0;
}
Visual Studio 2010 gives the following compilation errors
Code:
1>e:\my documents\dropbox\dropbox\programming\ebook projects\horton cpp\ex2_09\ex2_09\ex2_09.cpp(23): error C2332: 'enum' : missing tag name
1>e:\ex2_09.cpp(23): error C2236: unexpected 'class' 'main::Color'. Did you forget a ';'?
1>e:\ex2_09.cpp(23): error C3381: 'main::Color' : assembly access specifiers are only available in code compiled with a /clr option
1>e:\ex2_09.cpp(23): error C2062: type 'char' unexpected
1>e:\ex2_09.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\ex2_09.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\ex2_09.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\ex2_09.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\ex2_09.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\ex2_09.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\ex2_09.cpp(23): error C2143: syntax error : missing ';' before '}'
1>e:\ex2_09.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\ex2_09.cpp(24): error C2039: 'Blue' : is not a member of 'main::Color'
1> e:\ex2_09.cpp(23) : see declaration of 'main::Color'
1>e:\ex2_09.cpp(24): error C2065: 'Blue' : undeclared identifier
1>e:\ex2_09.cpp(30): error C2440: 'static_cast' : cannot convert from 'main::Color' to 'long'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>e:\ex2_09.cpp(34): error C2440: 'static_cast' : cannot convert from 'main::Color' to 'long'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
I'm really confused as to what is going on here.