Okay, strange question time!
I’m refactoring some old C++ code that declares a bunch of arrays like so:
static SomeStruct SomeStructArray[] = {
{1, 2, 3},
{4, 5, 6},
{NULL, 0, 0}
}
And so forth. These are scattered about in the source files, and are used right where they’re declared.
However, I would like to move them into a single source file (mostly because I’ve come up with a way of auto-generating them). And, of course, I naively try to make a header for them:
static SomeStruct SomeStructArray[];
Actually, even I know that’s wrong, but here’s the compiler error anyway:
error C2133: 'SomeStructArray' : unknown size arrays.h
error C2086: 'SomeStruct SomeStructArray[]' : redefinition arrays.cpp
So, I guess, what’s the right way to do this?
If you’re going to put the arrays themselves all in one file (and apparently access them from other files) you need to remove the
staticfrom the definitions (which makes them visible only inside the same translation unit (i.e., file)).Then, in your header you need to add an
externto each declaration.Finally, of course, you’ll need to ensure that when you have an array of
SomeStruct(for example), that the definition ofSomeStructis visible before you attempt to define an array of them.