Initializing static array of strings (C++)?

As the compiler say, you're trying to define a static member of MyClass that would be a const char* array named enumText. If you don't have it's declaration in the class, then there is a problem.

You should have :

class MyClass
{
   //blah
   static const char* enumText[];
};

or maybe you didn't want a static member. If not, you shouldn't have to use a class in the name.

Anyway, that has nothing to do with array initialization.


Given the error message, it seems to me that you have a declaration of MyClass somewhere (in another header maybe?) that doesn't have enumText[] declared in it. The error message indicates that the compiler knows about MyClass, but it doesn't know about the enumText member.

I'd look to see if you have multiple declarations of MyClass lurking in the shadows.

Can you get wintermute's or T.E.D.'s examples to compile?


This compiles with gcc version 4.0.1:

#include <iostream>

class MyClass {
public:
    const static char* enumText[];
};

const char* MyClass::enumText[] = { "a", "b", "c" };

int main()
{
    std::cout << MyClass::enumText[0] << std::endl;
}

Compiled with:

g++ -Wall -Wextra -pedantic s.cc -o s

Are you sure that MyClass::enumText is referencing the right class?


This code compiles:

struct X {
   static const char* enumtext[];
};

const char* X::enumtext[] = { "A", "B", "C" };

Check your code and find differences. I can only think that you did not define the static attribute in the class, you forgot to include the header or you mistyped the name.