初始化字符串的静态数组(C++)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1376264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:45:56  来源:igfitidea点击:

Initializing static array of strings (C++)?

c++arraysstringstaticinitialization

提问by Paul D.

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:

但是 gcc 给了我以下错误:

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

'const char* MyClass::enumText[]' 不是 'class MyClass' 的静态成员

What am I doing wrong? Thanks!

我究竟做错了什么?谢谢!

回答by David Rodríguez - dribeas

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.

检查您的代码并找出差异。我只能认为您没有在类中定义静态属性,您忘记包含标题或输入错误名称。

回答by E.M.

This compiles with gcc version 4.0.1:

这与 gcc 版本 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::enumTextis referencing the right class?

您确定MyClass::enumText引用了正确的类吗?

回答by Michael Burr

Given the error message, it seems to me that you have a declaration of MyClasssomewhere (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 enumTextmember.

鉴于错误消息,在我看来,您在MyClass某处(可能在另一个标题中?)有一个声明,其中没有声明 enumText[] 。错误消息表明编译器知道MyClass,但不知道enumText成员。

I'd look to see if you have multiple declarations of MyClasslurking in the shadows.

我想看看你是否有多个MyClass潜伏在阴影中的声明。

Can you get wintermute'sor T.E.D.'sexamples to compile?

你能得到WintermuteTED 的例子来编译吗?

回答by Klaim

As the compiler say, you're trying to define a static member of MyClassthat would be a const char*array named enumText. If you don't have it's declaration in the class, then there is a problem.

正如编译器所说,您正在尝试定义一个静态成员,MyClass该成员将是一个const char*名为enumText. 如果你在类中没有它的声明,那么就有问题了。

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.

无论如何,这与数组初始化无关。

回答by T.E.D.

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

以下代码在 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.

如果我不得不胡乱猜测,我会说您的初始化行在一个单独的源文件中,而您忘记 #include 包含 MyClass 的 .h 文件。这正是我搞砸并一直做的事情。

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.

此外,在我看来,您很可能在错误的位置使用了 const(或者它们不够多)。你现在拥有它的方式,不是常量数组,也不是数组中的指针;只是其中的字符元素。