I'm having a problem with std::max
. I can't figure it out.
int border = 35;
int myInt = 2;
int myOtherInt = 3;
int z = std::max(myInt + 2 * border, myOtherInt + 2 * border);
I've included the algorithm standard header. When I mouse over max, I am getting:
Error: expected an identifier
And a compile errors of:
error C2589:
'('
: illegal token on right side of'::'
error C2059: syntax error :'::'
What is wrong?
Hazarding a guess, since you're using VC++ – put this before any #include
s:
#define NOMINMAX
windows.h
defines macros named min
and max
like so:
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define max(a,b) (((a) > (b)) ? (a) : (b))
The Windows SDK has contained these macros since before C++ was standardized, but because they obviously play havoc with the C++ standard library, one can define the NOMINMAX
macro to prevent them from being defined.
As a rule, if you're using C++ (as opposed to C) and including windows.h
, always define NOMINMAX
first.
If you're on VC++, you can either use #define NOMINMAX
prior to including any headers, or do (std::max)(myInt + 2 * border, myOtherInt + 2 * border)
I would say that either max
is #define
's to something else or you need to explicitly invoke the template via std::max<int>
.
The "using" declaration (see using Declaration) is yet another way to work around the issue:
int border = 35;
int myInt = 2;
int myOtherInt = 3;
using std::max;
int z = max(myInt + 2 * border, myOtherInt + 2 * border);
It allows using std::max without explicit qualification.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With