tags:

views:

65

answers:

5

I have read this document http://stackoverflow.com/questions/1376264/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

+5  A: 

That should be:

const  char* MyClass::enumText[]={"a","b","c","d"};
// You forgot these           ^^
R Samuel Klatchko
Shouldn't this actually be `{'a','b','c','d'};`?
John Dibling
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.
John Dibling
+1  A: 

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

Mike G.
Or, more clearly, what R Samuel said :)
Mike G.
+3  A: 

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

Mark B
+3  A: 

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

a1ex07
+1  A: 
#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;
}
Franci Penov