views:

51

answers:

2

Following code is in my c++ class

static const  QString ALARM_ERROR_IMAGE ;

i want to initilize

ALARM_ERROR_IMAGE          = "error.png";

Is it possible to initilize error.png to static const QString ALARM_ERROR_IMAGE Want to keep it inside class

+2  A: 

Static variable of a class have to be defined explicitly in the namespace scope only once (irrespective of wheter they are further cv qualified or not).

In the .cpp file (e.g in <ClassName>.cpp), in the global namespace (assuming your class is in global namespace), define it as follows (assuming an appropriate constructor exists in QString)

NB: I missed 'const' in the definition below

const QString <ClassName>::ALARM_ERROR_IMAGE = "error.png";

$9.4.2/2 - "The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void. The definition for a static data member shall appear in a namespace scope enclosing the member’s class definition. In the definition at namespace scope, the name of the static data member shall be qualified by its class name using the :: operator."

Chubsdad
so can i write it outside the class like how u said
My class is abc.cpp and is it possible to write QString <abc>::ALARM_ERROR_IMAGE = "error.png"; if i do like this will be constant
You need to write as QString abc::ALARM_ERROR_IMAGE = "error.png";. Yes, once it is initialized, it can not be modified. It is constant.
Chubsdad
When i give constant its getting error.. Without const its working...
Inside class how i have to write..
A: 

Not possible to keep inside. Only const static integral data members are allowed to initialized inside a class or struct.

Santosh kumar