我已经阅读了这份文件
Initializing static array of strings (C++)?,并尝试在我的编译器中测试是否一切正常,这里是代码副本
#include <iostream>
#include <string>
using namespace std;
class MyClass {
public:
const static char* MyClass::enumText[];
};
const char* MyClass::enumText={"a","b","c","d"};
int main(){
std::cout<<MyClass::enumText[0]<<endl;
return 0;
}
但是这里有错误
1>c:\users\david\documents\visual studio 2010\projects\class_static\class_static.cpp(9): error C2372: 'enumText' : redefinition; different types of indirection
1> c:\users\david\documents\visual studio 2010\projects\class_static\class_static.cpp(7) : see declaration of 'enumText'
1>c:\users\david\documents\visual studio 2010\projects\class_static\class_static.cpp(9): error C2078: too many initializers
我正在使用Visual C++ 2010,为什么这样的错误出了什么问题?请帮助
最佳答案
#include <iostream>
#include <string>
using namespace std;
class MyClass
{
public:
const static char* enumText[];
};
const char* MyClass::enumText[] = {"a","b","c","d"};
int main()
{
std::cout<<MyClass::enumText[0]<<endl;
return 0;
}
关于c++ - 无法访问类中的const静态成员,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3533655/