I am sorry for my newbie question, but I do not know much about C++. Can anybody answer why I get the error “error: a call to a constructor cannot appear in a constant-expression” when compiling following code;
class EliminationWeight
{
public:
typedef double Type;
static const Type MAX_VALUE = __DBL_MAX__;
static const Type MIN_VALUE = -__DBL_MAX__;
};
I use Ubuntu 12.04 and gcc that comes with it. It is not my code and I know that this code probably it works OK 100% (perhaps in older version of gcc or other compiler). Is there a quick way to fix it?
Thanks in advance for any answers, this is actually my first time asking something at SO.
Call to a constructor cannot appear in a constant-expressionis a GCC error message which doesn’t really make sense to me here. Clang, for instance, accepts your code with some warnings:Anyway, initializing double in a class body is non-standard. You should do initialization separately:
and then in exactly one source file (not a header file):
In general, you can only initialize static member variables having integral types in class body, although this has been extended in C++ 0x11. See also Initializing const member within class declaration in C++