tags:

views:

573

answers:

8

I'm seeing strange errors when my C++ code has min() or max() calls. I'm using Visual C++ compilers.

+9  A: 

Check if your code is including the windows.h header file and either your code or other third-party headers have their own min()/max() definitions. If yes, then prepend your windows.h inclusion with a definition of NOMINMAX like this:

#define NOMINMAX
#include <windows.h>
Ashwin
A: 

This is officially the oddest question on Stack Overflow

eplawless
+1  A: 

Maybe you could post some code showing how you're using these and some info about the difference between what you expected would happen versus what actually happened?

Chris Farmer
+1  A: 

I deleted my original response when I saw you post your answer. Good one! :-)

I also wanted to see some example code (I was suspecting a logic error, since the question seemed vague). But now that I see the solution, I understand the reason for posing the question the way you did. It's one of those issues...the kind you go nuts trying to solve until you finally hit the answer, then you bang your head on your desk for ten minutes.

Wing
+3  A: 

Ugh... scope it, dude: std::min(), std::max().

Pat Notz
Ferruccio
A: 

I haven't used it in years but from memory boost assigns min and max too, possibly?

Orion Edwards
+1  A: 
itj
A: 

Honestly, when it comes to min/max, I find it best to just define my own:

#define min(a,b) (a < b ? a : b)
#define max(a,b) (a >= b ? a : b)
dhorn
Which, frankly, is asking for trouble. In C++, use `using std::swap` and write your own swap when you can do better than the default. In C, at the very lease write `#define min(a,b) ((a) < (b) ? (a) : (b))` and MAKE SURE YOU DON'T CALL IT WITH ANYTHING WITH SIDE EFFECTS, because you will have multiple evaluation.
David Thornley
Can you elaborate on your comment re: std::swap ?
dhorn