English 中文(简体)
warning C4003 and errors C2589 and C2059 on: x = std::numeric_limits<int>::max();
原标题:

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); ?

最佳回答

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.

问题回答

Other solution would be to wrap function name with parenthesis like this: (std::numeric_limits<int>::max)(). Same applies to std::max.

Not sure it s good solution for this... NOMINMAX is better IMO, but this could be an option in some cases.

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();
#ifdef max
#pragma push_macro("max")
#undef max
#define _restore_max_
#endif

#include <limits>

//... your stuff that uses limits

#ifdef _restore_max_
#pragma pop_macro("max")
#undef _restore_max_
#endif

(std::numeric_limits::max)()

Easy as pie.

Its definition in for me in Visual Studio 2013 (formatted for better spacing...) is as follows:

static _Ty (max)() _THROW0()
{   // return maximum value
    return (FLT_MAX);
}

So I m just using FLT_MAX. :) This may not be a universal solution, but it works well in my case, so I thought I would share.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签