I just stumbled across this exact same problem. The code we are discussing is part of the Contraction Hierarchies implementation by KIT.
This is the only compilation error given when trying to compile the code with gcc 4.8.
The fix suggested by @vitaut is what is needed to make this work. However, note that there are already the following lines lingering in main.cpp:
// doesn't look nice, but required by the compiler (gcc 4)
...
const EliminationWeight::Type EliminationWeight::MAX_VALUE;
const EliminationWeight::Type EliminationWeight::MIN_VALUE;
If you decide to create an EliminationWeight.cpp file to go along with your EliminationWeight.h and include it in the Makefile, these lines are the reason why you are seeing a different error than mentioned above:
main.cpp:86:31: error: uninitialized const ‘EliminationWeight::MAX_VALUE’ [-fpermissive]
const EliminationWeight::Type EliminationWeight::MAX_VALUE;
^
main.cpp:87:31: error: uninitialized const ‘EliminationWeight::MIN_VALUE’ [-fpermissive]
const EliminationWeight::Type EliminationWeight::MIN_VALUE;
^
The solution is to either remove these lines in main.cpp or use them for the actual initialization. I have gone with the latter and the lines now look like this:
const EliminationWeight::Type EliminationWeight::MAX_VALUE = std::numeric_limits< EliminationWeight::Type >::max();
const EliminationWeight::Type EliminationWeight::MIN_VALUE = -std::numeric_limits< EliminationWeight::Type >::max();
Note that I have used the std::numeric_limits
template for the type defined by the EliminationWeight::Type
typedef. This means we only need to change that typedef to use a different type.
However, using these in main.cpp mandates that we include the header for the numeric_limits
template. It also worked for me without including the header but that is probably because it is included via some other included file. For the purpose of clean code we should include it anyway.
#include <limits>
Also note that C++11 provides a new function lowest
for the numeric_limits
template meaning you could substitue the last line with the following:
const EliminationWeight::Type EliminationWeight::MIN_VALUE = std::numeric_limits< EliminationWeight::Type >::lowest();
However, the C++ reference on lowest
specifies the return value for floating point types as
implementation-dependent; generally, the negative of max()
so I am not sure whether you gain much by using this function. It does give you cleaner looking code but it seems the return value is somewhat unspecified.