I get the following errors on the line with "static const string ...":

error C2059: syntax error : '{'
error C2334: unexpected token(s) preceding '{'; skipping apparent function body

when trying to compile something like this:

#ifndef TEST_H
#define TEST_H

#include <string>
using namespace std;

class Test
{
public:
    static const string MYARRAY[] = {"hello", "world"};
};

#endif

I've been messing around with the code for a while now, and can't seem to wrap my head around what's wrong. The header file I'm working on is holding several configurable variables, and are available to the entire project. I'm hoping to keep them static, constant, and initialized at runtime.

Any help is appreciated. Thanks all!

Recommended Answers

All 7 Replies

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!

Thanks a lot Duoas ... worked perfectly.

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.

Thanks again!

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.

> 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

Thanks guys, you've been great help!

> 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

CardGame( )
	{
		for( int i=0;i<13;i++ )
			no[i] = i+1;
		//suit[ 0 ]= { "hearts" , "clubs" , "diamond" , "spades" };
	}
	void print()
	{
		for( int i=0;i<13;i++ )
		{
			cout<<no[i]<<" of"<<suit[ i%4 ]<<endl;
		}
	}
private:
	int no[ 13 ];
	static const string suit[ 4 ] = { "hearts" , "clubs" , "diamond" , "spades" };

it gives the same error . do i have to seperate the header and cpp files

commented: Don't bump old threads. -1

@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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.