C++ 警告 C4003 和错误 C2589 和 C2059 在:x = std::numeric_limits<int>::max();

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1904635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:33:41  来源:igfitidea点击:

warning C4003 and errors C2589 and C2059 on: x = std::numeric_limits<int>::max();

c++visual-c++maxnumeric-limits

提问by Harvey

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

为什么将 max 视为宏 max(a,b); ?

回答by Steve Guidi

This commonly occurs when including a Windows header that defines a minor maxmacro. If you're using Windows headers, put #define NOMINMAXin your code, or build with the equivalent compiler switch (i.e. use /DNOMINMAXfor Visual Studio).

这通常发生在包含定义minmax宏的 Windows 标头时。如果您使用的是 Windows 头文件,请#define NOMINMAX输入您的代码,或使用等效的编译器开关进行构建(即对 Visual Studio使用/DNOMINMAX)。

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

请注意,NOMINMAX使用构建禁用宏在整个程序中的使用。如果您需要使用minormax操作,请使用std::min()std::max()来自<algorithm>标题。

回答by denis-bu

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

其他的解决办法是换函数名这样的括号:(std::numeric_limits<int>::max)()。同样适用于std::max.

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

不确定这是一个很好的解决方案...... NOMINMAX 是更好的 IMO,但在某些情况下这可能是一个选择。

回答by R Samuel Klatchko

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

其他一些头文件正在使用 max 宏污染全局名称空间。您可以通过取消定义宏来解决这个问题:

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

回答by dmjalund

#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

回答by Andrew

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

它在 Visual Studio 2013 中对我的定义(格式化为更好的间距...)如下:

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.

所以我只是使用 FLT_MAX。:) 这可能不是一个通用的解决方案,但在我的情况下效果很好,所以我想我会分享。

回答by Mattias Bergmark

(std::numeric_limits::max)()

(std::numeric_limits::max)()

Easy as pie.

非常简单。