views:

493

answers:

2

This line works correctly in a small test program, but in the program for which I want it, I get the following compiler complaints:

#include <limits>

x = std::numeric_limits<int>::max();

c:\...\x.cpp(192) : warning C4003: not enough actual parameters for macro 'max'
c:\...\x.cpp(192) : error C2589: '(' : illegal token on right side of '::'
c:\...\x.cpp(192) : error C2059: syntax error : '::'

I get the same results with:

#include <limits>
using namespace std;

x = numeric_limits<int>::max();

Why is it seeing max as the macro max(a,b); ?

+7  A: 

Some other header file is polluting the global name space with a max macro. You can fix that by undefining the macro:

#undef max
x = std::numeric_limits<int>::max();
R Samuel Klatchko
Very good. That fixed it. Thanks.
Harvey
+6  A: 

This commonly occurs when including a Windows header that defines a min or max macro. If you're using Windows headers, put #define NOMINMAX in your code, or build with the equivalent compiler switch (i.e. use /DNOMINMAX for Visual Studio).

Note that building with NOMINMAX disables use of the macro in your entire program. If you need to use the min or max operations, use std::min() or std::max() from the <algorithm> header.

Steve Guidi
Okay, I just have to ask...Can I have both in the same file?x = std::numeric_limits<int>::max();// some tricky preprocessor commandc = max(a,b);
Harvey
@Harvey: I've editted my answer to address your usage of max() and macro max() in one file.
Steve Guidi
I do use min() and max() in other files in this project and using precompiled headers, it is disabled for all files.#undef max works for my case and is only effective for the rest of the file it is in.
Harvey
@Harvey: #undef affects the rest of the entire translation unit (very different from "rest of the file it is in"), can lead to results highly dependent on include order, and may interfere with precompiled headers. This answer is the preferred solution. Macros like min and max cause complicated problems in what should be easy. Macros are evil in C++.
Roger Pate