初始化字符串(C ++)的静态数组? [英] Initializing static array of strings (C++)?

查看:137
本文介绍了初始化字符串(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" };

但是GCC给了我以下错误:

However gcc gives me the following error:

为const char * MyClass的:: enumText []'是不是'MyClass类

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

我是什么做错了吗?谢谢!

What am I doing wrong? Thanks!

推荐答案

这code编译:

struct X {
   static const char* enumtext[];
};

const char* X::enumtext[] = { "A", "B", "C" };

检查code和寻找差异。我只能认为你没有定义在类中的静态属性,你忘了,包括页眉或输入了错误的名称。

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.

这篇关于初始化字符串(C ++)的静态数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆