C++11 Enum forward causes "underlying type mismatch"
There doesn't seem to be any way of doing this, even if you specify the exact same underlying type that the compiler would have chosen for your C++03-style enum
.
Example: compiling the following code...
enum Foo { A=1 };
cout << typeid(typename std::underlying_type<Foo>::type).name();
...on Coliru and demangling via c++filt
will print "unsigned int"
both with g++ and clang++.
Even if you specify unsigned int
as the explicit underlying type of your Foo
forward declaration, both compilers will complain.
enum Foo : unsigned int;
void bar(Foo);
enum Foo {A=1};
main.cpp:8:6: error: enumeration previously declared with fixed underlying type
enum Foo {A=1};
^
main.cpp:5:6: note: previous declaration is here
enum Foo : unsigned int;
^
This is because both the forward declaration and the "real" enum
declaration need to have the same explicit underlying type, even if you manage to "guess" what the compiler would have chosen for you.
tl;dr: you can only forward-declare an enum
if both the forward-declaration and the real declaration have the same explicitly specified underlying type.
You can only forward declare an enum if you give it a fixed underlying type in the forward declaration. Also, the definition of the enum must use the same fixed underlying type.
Your problem is that your enum definition in header.h
doesn't have an underlying type, but the later forward declaration has one. They both must have one.