Reputation: 707
I'd like to use a enum class in the following way
// Foo.h
enum class Foo : uint8_t;
with
// Foo.cpp
enum class Foo : uint8_t {
FOO = 0,
};
// Bar.cpp
enum class Foo : uint8_t {
FOO = 1,
};
and
// MainLinkedWithFoo.cpp
#include "Foo.h"
int main () {
(void)Foo::FOO;
return 0;
}
// MainLinkedWithBar.cpp
#include "Foo.h"
int main () {
(void)Foo::FOO;
return 0;
}
But I get following error
incomplete type 'Foo' named in nested name specifier
Does somebody know how to solve this "problem"?
Thanks
Upvotes: 0
Views: 1461
Reputation: 38277
You can forward-declare scoped enums, but at the time you refer to the actual enumeration identifiers (e.g. Foo::FOO
), the complete enum definition must be available to the compiler.
Most often, scoped enums are neither an implementation details passed through interfaces nor a bottleneck for your compile times. Hence, the straightforward fix is to put the definition into the header file.
// Foo.h
enum class Foo : uint8_t {
FOO = 0,
BAR = 1
};
But as you explicitly asked for separate declaration and definition, you might want this:
// FooFwd.h
enum class Foo : uint8_t;
and the definition in Foo.h
as above. Then, whenever you want to use an enumeration instance without referring its actual enumerators, you can just #include "FooFwd.h"
, and if you need access to e.g. Foo::FOO
, go with #include "Foo.h"
.
Upvotes: 1