50

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++

Community
  • 1
  • 1
NaturalDemon
  • 934
  • 1
  • 9
  • 21
  • Anything that includes `windows.h` and uses the algorithm versions should have it defined before the include if it isn't defined already. – chris Nov 16 '12 at 12:17
  • 4
    Why not post the code on the files where it isn't working, instead of posting the code on the file where it is working! Really, we can't see your code unless you post it. – john Nov 16 '12 at 12:19
  • 3
    And you're sure those *"other files"* don't include `` as well without defining `NOMINMAX` previously (maybe indirectly through some other header)? – Christian Rau Nov 16 '12 at 12:23
  • @ john, it's a 5 line frametimer code, nothing special. thank you – NaturalDemon Nov 16 '12 at 22:03
  • special note; its worth noting that you can `#undef NOMINMAX` after the `#include `. This would reduce so much confusion. same for `#define WIN32_LEAN_AND_MEAN`. `#define NOMINMAX` acts as an "argument" to Windows.h in a funny way. and `#undef NOMINMAX` is like cleaning up the stack afterwards, in a funny way. Future includes always thank you for it by not spamming warnings. – Dmytro Jul 31 '17 at 16:55

5 Answers5

107

If you're really desperate, put parentheses around the function names:

(std::min)(x, y);

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 '('.)

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
37

Define NOMINMAX via a compiler flag:

> cl.exe -DNOMINMAX ...

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 :

Simply define the NOMINMAX preprocessor symbol. This can be done in the Developer Studio project under Build, Settings, on the C/C++ tab, in the Preprocessor category. This will suppress the min and max definitions in Windef.h.

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • 9
    Define `NOMINMAX` via a compiler flag: > cl.exe -DNOMINMAX ... causes about 12 of the following lines ... 1>c:\program files\microsoft sdks\windows\v7.0a\include\gdiplustypes.h(470): error C3861: 'min': identifier not found i´m using the GDI stuff. thank you – NaturalDemon Nov 16 '12 at 22:05
  • 2
    To **NaturalDemon**, here you have solution for your problem -- http://stackoverflow.com/a/4914108/867349 – NG_ Feb 05 '15 at 19:00
14

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:

#define NOMINMAX

// Include C++ headers
#include <algorithm>
using namespace std;

// Include Windows headers
#include <windows.h>
#include <gdiplus.h>
David Gausmann
  • 1,570
  • 16
  • 20
  • 23
    Or if you do not want to pollute the global namespace as much, `using std::min; using std::max;` may be a better solution. – j_schultz Dec 11 '17 at 00:13
  • 3
    `std::min/max`, unlike the macros, require both arguments to be of the same type. This may not always work with all libraries that use the macros. A better solution is to not define `NOMINMAX`, include `Windows.h` and all libraries that need those macros, then `#undef min/max`. Thus the included libraries work and there's no global namespace pollution. – Yakov Galka Apr 10 '20 at 17:58
7

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:

#ifndef NOMINMAX
#define min(x,y) ((x) < (y) ? (x) : (y))
#define max(x,y) ((x) > (y) ? (x) : (y))
#endif

So #define NOMINMAX is telling the compiler (or actually the preprocessor) to skip over the definitions of min and max, but it will only apply if you do it before you #include "windows.h".

IInspectable
  • 46,945
  • 8
  • 85
  • 181
Tom
  • 7,269
  • 1
  • 42
  • 69
  • In the code of the question, `#define NOMINMAX` does appear before `#include `. This has also been the case in the original version of the question. However, you are correct that OP may have put this line in a different place in the other source files, as OP is not showing us the code of the files that are not working. – Andreas Wenzel May 29 '23 at 16:47
5

In Visual Studio, Adding 'NOMINMAX' to C++ preprocessor properties fixed my issue.

Open project properties -> C/C++ -> Preprocessor -> Preprocessor Definitions -> Add 'NOMINMAX'.

KRG
  • 655
  • 7
  • 18