我已阅读此文件 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以及为什么会出现这样的错误?请帮助
答案 0 :(得分:5)
应该是:
const char* MyClass::enumText[]={"a","b","c","d"};
// You forgot these ^^
答案 1 :(得分:3)
您忘记了变量定义中的[]
:const char* MyClass::enumText[]={"a","b","c","d"};
答案 2 :(得分:3)
const char* MyClass::enumText[]={"a","b","c","d"};
答案 3 :(得分:1)
我认为你只是错过了你的enumText定义末尾的[](就在= {...)之前。
答案 4 :(得分:1)
#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;
}