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!
A more convenient way to initialize a C string is to initialize it through character array: char char_array[] = "Look Here"; This is same as initializing it as follows: char char_array[] = { 'L', 'o', 'o', 'k', ' ', 'H', 'e', 'r', 'e', '\0' };
Highlights: There are four methods of initializing a string in C: Assigning a string literal with size. Assigning a string literal without size. Assigning character by character with size. Assigning character by character without size.
String array can be initialized using the new keyword. We cannot initialize string array without specifying it’s the size. There are two ways to initialize a string array. 1. At the time of declaration: string [] variable_name = new string [ size ]; 2. After declaration: string [] variable_name;
By using the String class object: By using a string keyword: 2. Declaration without size String array can be initialized using the new keyword. We cannot initialize string array without specifying it’s the size. There are two ways to initialize a string array. 1. At the time of declaration: 2. After declaration:
An initializer list initializes elements of an array in the order of the list. This initializes an array of size 5, with the elements {1, 2, 3, 4, 5} in order. This means that arr [0] = 1, arr [1] = 2, and so on. We don’t need to initialize all the elements 0 to 4. We can even do only from indices 0 to 2.
And you are initializing an object that has an aggregate type, so the value must be known at compile time and the address of automatic variables are not in that case. All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.
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.
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?
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With