Search code examples
c++arraysstringstaticinitialization

Initializing static array of strings (C++)?


I can't for the life of me figure out how to do this properly. I have a class that needs to store some constants (text that corresponds to values in an enum type) - I have it declared like this (publicly) in my class:

const static char* enumText[];

And I'm trying to initialize it like this:

const char* MyClass::enumText[] = { "A", "B", "C", "D", "E" };

However gcc gives me the following error:

'const char* MyClass::enumText[]' is not a static member of 'class MyClass'

What am I doing wrong? Thanks!


Solution

  • 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.