I'd like to define a constant char* in my header file for my .cpp file to use. So I've tried this:
private:
static const char *SOMETHING = "sommething";
Which brings me with the following compiler error:
error C2864: 'SomeClass::SOMETHING' : only static const integral data members can be initialized within a class
I'm new to C++. What is going on here? Why is this illegal? And how can you do it alternatively?
You need to define static variables in a translation unit, unless they are of integral types.
In your header:
In the .cpp file:
C++ standard, 9.4.2/4:
Constant initializer allowed by C++ Standard only for integral or enumeration types. See 9.4.2/4 for details:
And 9.4.2/7:
So you should write somewhere in cpp file:
I do it all the time - especially for expensive const default parameters.