You can't initialize it inside the class definition.
Do this instead:
class Test
{
public:
static const string MYARRAY[];
};
const string Test::MYARRAY[] = {"hello", "world"};
Have fun!
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
Sorry, I can't. I haven't a clue why...
But I suspect it is just to be logical. Remember, a class is just a blueprint --it doesn't actually create any data (it only shapes it).
So it can link to static data in the class structure, but that static data must be declared as such: an actual const object taking space in the data segment.
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
> Could you refer me to a site that explains exactly why it can't be initialized inside the class definition? I'd like to read up on the working behind why this is.
class Test
{
public:
static const string MYARRAY[] ; // only a declaration
};
class Test
{
public:
static const string MYARRAY[] = {"hello", "world"}; // also a definition
};
A class definition is one of the things that is not within the ambit of ODR; these may be present in more than one transltion unit provided the definitions are 'identical'. 'Non-extern objects and functions in different translation units are different entities, even if their names and types are the same.' but there should be only one instance of a static member of a class. Test::MYARRAY therefore has external linkage, class Test can appear in more than one translation unit. allowing the definition of Test::MYARRAY within the class definition would violate ODR. http://en.wikipedia.org/wiki/One_Definition_Rule
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
@compeng
You cannot hijack another thread to ask your question but you have to start your own thread instead. Please do not resurrect old threads.
Have a look at forum rules .
Please read before posting - http://www.daniweb.com/forums/thread78223.html
Thread Closed.
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241