i recently added:
#define NOMINMAX
#include <Windows.h>
#include <algorithm>
to my main.cpp in order to use
std::max( x , x ); // x is just a placeholder and not actual anything
std::min( x , x );
but i can't use std::max()/std::min()
in other files.
error C2589: '(' : illegal token on right side of '::'
error C2059: syntax error : '::'
i tried to add #define NOMINMAX
in my other files, but fails. what is the clue?
i looked around before asking, but i don't understand the answer Possible problems with NOMINMAX on Visual C++
If you're really desperate, put parentheses around the function names:
This syntax won't apply a function-like macro. (Formally, to apply a function-like macro the name of the macro must be followed by optional white space then a '('.)
It's likely that your problem is that you
#define NOMINMAX
after you#include "windows.h"
. It is important that the#define
come first.The reason is that windows.h (actually I think windef.h, which is included by windows.h) has code similar to this:
So
#define NOMINMAX
is telling the compiler (or actually the preprocessor) to skip over the definitions ofmin
andmax
, but it will only apply if you do it before you#include "windows.h"
.Define
NOMINMAX
via a compiler flag:this will then be defined for all of the source files. I don't use the IDEs but this page provides guidance on navigating the IDE to set this: Using STL in Windows Program Can Cause Min/Max Conflicts :
If you define NOMINMAX, because you prefer the STL version, then you may get problems while including gdiplus.h, which uses the min/max macro. As solution you need to include the STL headers and use "using namespace std" before you include the gdiplus.h.
In example: