Reputation: 2661
I create an enum called Types
:
enum Types {Int,Double,String};
When I create an object and initialize it with one of the enum allowed values I get the following error: "Error: type name is not allowed".
Types ty = Types.Double;
Any ideas?
Upvotes: 10
Views: 40657
Reputation: 60979
In C++, there are two different types of enumerations - scoped and unscoped ones (the former was introduced with C++11). For unscoped ones the names of the enumerators are directly introduced into the enclosing scope.
N3337 §7.2/10
Each enum-name and each unscoped enumerator is declared in the scope that immediately contains the enum-specifier. Each scoped enumerator is declared in the scope of the enumeration. These names obey the scope rules defined for all names in (3.3) and (3.4).
Your enumeration is unscoped, therefore it suffices to write
Types ty = Double;
For scoped enumerations, as the name suggests, the enumerators are declared in the enumeration scope and have to be qualified with the enumeration-name:
enum class ScopedTypes {Int,Double,String};
enum UnscopedTypes {Int,Double,String};
ScopedTypes a = ScopedTypes::Double;
//ScopedTypes b = Double; // error
UnscopedTypes c = UnscopedTypes::Double;
UnscopedTypes d = Double;
Upvotes: 15
Reputation: 310990
Either use
Types ty = Double;
or
enum class Types {Int,Double,String};
Types ty = Types::Double;
Upvotes: 8
Reputation: 83527
The compiler is complaining about the attempt at qualifying the value Double
which is Java's way to do this.
Just do
Types ty = Double;
Upvotes: 4