error C2589: illegal token to the right of "(":"::"

created at 06-19-2022 views: 39

error C2589 Illegal token on right side of "(":"::"

error code

inline double 
getFitnessScore (double max_range = std::numeric_limits<double>::max());

The std::numeric_limits<double>::max() is wrong

problem causes

It is found on the Internet that the function template max conflicts with the global macro max in Visual C++

After goto, I found that max(), which should be a function, was located under minwindef.h

#ifndef NOMINMAX

#ifndef max
#define max(a,b)            (((a) > (b)) ? (a) : (b))
#endif

#ifndef min
#define min(a,b)            (((a) < (b)) ? (a) : (b))
#endif

#endif  /* NOMINMAX */

That is to say, when C++ compiles, it first parses the macro definition, and parses the std::numeric_limits<double>::max(), which should be a function, into a macro, and a conflict occurs.

solution

There are several methods recommended on the Internet, I turned around to solve this problem, made a record, and shared it with friends who have this problem as a reference

Method 1: Add parentheses when using min or max

First of all, this method is not recommended as in other tutorials, because in my case, the point of error is the file in the PCL library, try not to modify it

I also tried this. I recommend this post. The functions involved are std::min and std::max. I tried the method of adding parentheses recommended in the post, and I thought about changing the parentheses. location, but after joining, it has not been resolved, and it has been restored.

Method 2: Add #define NOMINMAX to the header file, or add NOMINMAX to the preprocessor After adding, the error is not resolved, and there are more

C3861 "min": identifier mfc_bin not found C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\GdiplusTypes.h

C3861 "max": identifier mfc_bin not found C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\GdiplusTypes.h 481

The point of the problem is to add GdiplusTypes.h, directly turn off min, the definition of max macro is still problematic

Method 3: #undef max and #undef min (valid for personal testing)

First find the location of the header file that reported the error, and advance #include <windows.h> to include it before the header file (critical step)

Then add it before the header file that contains the error

#undef max
#undef min

The purpose of these two sentences is to first compile the macro definitions min and max through windows.h, and then release the macro definitions of min and max, so that the min and max functions can be compiled and passed The header file that reported the error before.

created at:06-19-2022
edited at: 06-19-2022: