Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::max - expected an identifier

Tags:

c++

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?

like image 672
Jay Avatar asked Aug 12 '11 02:08

Jay


4 Answers

Hazarding a guess, since you're using VC++ – put this before any #includes:

#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.

like image 120
ildjarn Avatar answered Nov 15 '22 09:11

ildjarn


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)

like image 43
Dave S Avatar answered Nov 15 '22 07:11

Dave S


I would say that either max is #define's to something else or you need to explicitly invoke the template via std::max<int>.

like image 4
Jim Buck Avatar answered Nov 15 '22 07:11

Jim Buck


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.

like image 2
PeterN Avatar answered Nov 15 '22 07:11

PeterN