Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 780
Paul D. Avatar asked Sep 03 '09 22:09

Paul D.


People also ask

How do you initialize an array of strings in C?

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' };

What is initialization of string in C?

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.

How to initialize a string array in C++?

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;

How to initialize a string array without size in Java?

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:

What is an initializer list in C++?

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.

How to initialize an object that has static storage duration?

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.


Video Answer


3 Answers

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.

like image 160
David Rodríguez - dribeas Avatar answered Nov 15 '22 14:11

David Rodríguez - dribeas


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?

like image 33
E.M. Avatar answered Nov 15 '22 13:11

E.M.


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?

like image 37
Michael Burr Avatar answered Nov 15 '22 14:11

Michael Burr