Why I am getting this error: constexpr' is not valid here
Emily Wong
I have this code:
class myClass
{ constexpr int x = 4;
};and on visual studio 2015, I am getting this error:
'constexpr' is not valid hereWhy I am getting this error? I want a const static variable that I can initlaize it on header file.
In the next step I want to change my class to a template, but this constant is not related to the type of clas.
52 Answers
Non-static data members cannot be declared as constexpr. Use
class myClass
{ static constexpr int x = 4;
};instead.
I want a const static variable that I can initlaize it on header file
if your main concern is constant value which is shareable to all template type instances, then you can just change to the below:
class myClass
{ static const int x = 4;
};If your concern is the memory space (although it is shared between all instances), you can just use compilation pre-processing solution (i.e. #Define X 4)