enumclass animal : short ; // declaration
constchar* common_name(animal) ;
// animal a = animal::BUTTERFLY ; // *** error, animal has not been defined
enumclass animal : short { AARDVARK, BUTTERFLY, COYOTE } ; // definition
animal a = animal::BUTTERFLY ; // fine, animal has been defined
constchar* common_name( animal a )
{
switch(a) // fine, animal has been defined
{
case animal::AARDVARK : return"Aardvark" ;
case animal::BUTTERFLY : return"Butterfly" ;
case animal::COYOTE : return"Coyote" ;
default : return"This is terrible" ;
}
}
enumclass animal : short ; // declaration
void function(example e);
enumclass example : short
{
exampleOne,
exampleTwo,
exampleThree
};
int main()
{
example ex = example::exampleTwo;
function(ex);
return 0;
}
void function(example e)
{
cout << "function called. example: " << static_cast<short>(e) << endl;
}
I get the following compile errors:
1 2 3 4
main.cpp:4: error: variable or field 'function' declared void
main.cpp:4: error: 'example' was not declared in this scope
main.cpp: In function 'int main()':
main.cpp:16: error: 'function' was not declared in this scope
Ill post the forward declarations I tried in the next post to separate it
main.cpp:4: error: use of enum'example' without previous declaration
main.cpp:5: error: variable or field 'function' declared void
main.cpp:5: error: 'example' was not declared in this scope
main.cpp: In function 'int main()':
main.cpp:17: error: 'function' was not declared in this scope
And when I try to do a forward declaration like this:
enumclass example : short;
I get the following compilation errors:
1 2 3 4 5 6
main.cpp:4: error: expected '{' before ';' token
main.cpp:4: error: expected class-key before ';' token
main.cpp:5: error: variable or field 'function' declared void
main.cpp:5: error: 'example' was not declared in this scope
main.cpp: In function 'int main()':
main.cpp:17: error: 'function' was not declared in this scope
However, if I reorder it, and dont attempt a forward declaration of the strongly typed enums, like this, it works perfectly (but is restrictive).
Please show the full code snip when you are doing the forward declaration of the enum and are getting errors
main.cpp:4: error: expected '{' before ';' token
main.cpp:4: error: expected class-key before ';' token
main.cpp:5: error: variable or field 'function' declared void
main.cpp:5: error: 'example' was not declared in this scope
main.cpp: In function 'int main()':
main.cpp:17: error: 'function' was not declared in this scope