15

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!

3
  • Try: static const char* enumText[]; in the class? Commented Sep 3, 2009 at 22:20
  • I strongly suspect that this is some older g++ version that incorrectly treats const static char* [] there as static char* [] const rather than static const char* [] (because of static being in an "unusual" place). Commented Sep 3, 2009 at 22:45
  • Could you paste a complete and compilable piece of code into your answer which shows the problem? Commented Sep 4, 2009 at 7:58

5 Answers 5

17

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Line 5 should have X::enumtext or else you are just creating a new variable.
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)
How would you do this if the data type was std::string instead of char*?
@gone then you'd probably want to use a vector<string> ... = {"str1","str2"}; etc.
6

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?

Comments

2

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?

Comments

2

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.

2 Comments

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

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.