2

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

1
  • oops sorry guys i am tired today and make such cool mistake sorry i have missed brackets
    – user466534
    Commented Aug 20, 2010 at 18:17

5 Answers 5

5

That should be:

const  char* MyClass::enumText[]={"a","b","c","d"};
// You forgot these           ^^
2
  • Shouldn't this actually be {'a','b','c','d'};? Commented Aug 20, 2010 at 18:27
  • Nevermind, I see now he wants an array of char pointers, not an array of char. I'll leave this up nonetheless in case others are cornfuzed. Commented Aug 20, 2010 at 18:30
3

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

3

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

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

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

0