Reputation: 23
I've got a pair of classes. One of them has a set of static const uint32_t's (the following code is a trimmed down example):
class Foo {
public:
static const uint32_t BAZ;
void RunMe(void);
};
class Bar {
public:
void RunMeAlso(void);
};
I'm doing the usual thing of one class per cpp file, since the actual classes are pretty sizeable, but I've struck a problem. Both Foo::RunMe()
and Bar::RunMeAlso()
access the value Foo::BAZ
.
I've got the definition of const uint32_t Foo::BAZ = 1;
in foo.cpp, but hit a compile error with it in bar.cpp (where it is used as a case label in a switch statement) in VS2010 (tho this code needs to be portable, but I haven't yet checked it with gcc). Both header files are included in both source files... and trying to put some kind of extern reference in bar.cpp to tell the compiler that it's in a different translation unit doesn't solve it (creates more compile errors)... and of course all the examples I've found on this kind of subject are for the trivial case of single classes/files where someone didn't know/forgot to use the definition in addition to the declaration.
Thoughts, anyone, on how to resolve this? (I would prefer not having to use an enum but will if that's the only other option since I'm sure that will compile!)
TIA,
-J
Upvotes: 2
Views: 744
Reputation: 98398
Case labels must be compiler constants. A static member variable is not a compiler constant, unless it is constant and initialized in the declaration with a constant expression.
So:
static const uint32_t BAZ = 1;
in the header; orif
instead of switch
.BTW, it is usually helpful to read the actual error message. And post it, if you are going to ask about it...
Upvotes: 3