开发者

Unable to access const static members in class

I have read this document Initializing static array of strings (C++)? and tried to test in my compiler if everything would be fine here is copy of code

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

but here is mistakes

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

i am using visual c++ 2010 and why such mistakes what is wrong?please help


That should be:

const  char* MyClass::enumText[]={"a","b","c","d"};
// You forgot these           ^^


You forgot the [] in the definition of the variable: const char* MyClass::enumText[]={"a","b","c","d"};


You missed []. It should be const char* MyClass::enumText[]={"a","b","c","d"};


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


I think you're just missing the [] on the end of your definition of enumText (right before the ={...).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜