Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning C4003 and errors C2589 and C2059 on: x = std::numeric_limits<int>::max();

This line works correctly in a small test program, but in the program for which I want it, I get the following compiler complaints:

#include <limits>  x = std::numeric_limits<int>::max();  c:\...\x.cpp(192) : warning C4003: not enough actual parameters for macro 'max' c:\...\x.cpp(192) : error C2589: '(' : illegal token on right side of '::' c:\...\x.cpp(192) : error C2059: syntax error : '::' 

I get the same results with:

#include <limits> using namespace std;  x = numeric_limits<int>::max(); 

Why is it seeing max as the macro max(a,b); ?

like image 675
Harvey Avatar asked Dec 15 '09 01:12

Harvey


People also ask

What is Numeric_limits int >:: max ()?

std::numeric_limits::max(): The std::numeric_limits<T>::max() function is used to get the maximum finite value representable by the numeric type T. All arithmetic types are valid for type T.

What does Numeric_limits mean in C++?

std::numeric_limits ::digits in C++ with Example Return Value: The function std::numeric_limits<T>::digits returns the number of radix digits that the type can represent without loss of precision.


2 Answers

This commonly occurs when including a Windows header that defines a min or max macro. If you're using Windows headers, put #define NOMINMAX in your code, or build with the equivalent compiler switch (i.e. use /DNOMINMAX for Visual Studio).

Note that building with NOMINMAX disables use of the macro in your entire program. If you need to use the min or max operations, use std::min() or std::max() from the <algorithm> header.

like image 195
Steve Guidi Avatar answered Oct 01 '22 02:10

Steve Guidi


Other solution would be to wrap function name with parenthesis like this: (std::numeric_limits<int>::max)(). Same applies to std::max.

Not sure it's good solution for this... NOMINMAX is better IMO, but this could be an option in some cases.

like image 43
denis-bu Avatar answered Oct 01 '22 03:10

denis-bu