views:

1272

answers:

5

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!

+1  A: 

As the compiler say, you're trying to define a static memeber 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 did'nt want a static member. If not, youy shouldn't have to use a class in the name.

Anyway, that have nothing to do with array intialization.

Klaim
Op says "I have it declared like this (publicly) in my class"
Snarfblam
@Snarfblam: but often what an OP says and reality are 2 different things - especially since what he says he's done should work.
Michael Burr
A: 

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.

David Rodríguez - dribeas
Line 5 should have `X::enumtext` or else you are just creating a new variable.
Whisty
That was a typo in the answer, I have corrected it. Anyway, the code does compile with gcc (both 4.0 in MacOSX and 4.3 in ubuntu)
David Rodríguez - dribeas
Wooo I feel stupid, I had a typo that I didn't notice..
Paul D.
There are two of us.
David Rodríguez - dribeas
A: 

The following code compiles just fine for me in VS 2005:

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

If I had to take a wild guess, I'd say that your initilization line is in a separate source file, and you forgot to #include the .h file that contains MyClass. That's exactly the kind of thing I screw up and do all the time.

Also, it seems quite likely to me that you have the const in the wrong spot (or not enough of them). The way you have it now, it isn't the array that is constant, or the pointers in the array; just the character elements within it.

T.E.D.
+4  A: 

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?

Whisty
+2  A: 

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?

Michael Burr