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?
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;
Either use
Types ty = Double;
or
enum class Types {Int,Double,String};
Types ty = Types::Double;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With