C++ std::max - 需要一个标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7035023/
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-28 16:23:27  来源:igfitidea点击:

std::max - expected an identifier

c++

提问by Jay

I'm having a problem with std::max. I can't figure it out.

我有问题std::max。我想不通。

int border = 35;
int myInt = 2;
int myOtherInt = 3;
int z = std::max(myInt + 2 * border, myOtherInt + 2 * border);

I've included the algorithm standard header. When I mouse over max, I am getting:

我已经包含了算法标准标题。当我将鼠标悬停在 max 上时,我得到:

Error: expected an identifier

错误:需要一个标识符

And a compile errors of:

和一个编译错误:

error C2589: '(': illegal token on right side of '::'
error C2059: syntax error : '::'

错误 C2589:错误 C2059'('右侧的非法标记'::'
:语法错误:'::'

What is wrong?

怎么了?

回答by ildjarn

Hazarding a guess, since you're using VC++ – put this before any #includes:

危险的猜测,因为您使用的是 VC++ - 将其放在任何#includes之前:

#define NOMINMAX

windows.hdefines macros named minand maxlike so:

windows.h定义命名的宏minmax如下所示:

#define min(a,b)            (((a) < (b)) ? (a) : (b))
#define max(a,b)            (((a) > (b)) ? (a) : (b))

The Windows SDK has contained these macros since before C++ was standardized, but because they obviously play havoc with the C++ standard library, one can define the NOMINMAXmacro to prevent them from being defined.

Windows SDK 在 C++ 标准化之前就包含了这些宏,但由于它们显然对 C++ 标准库造成了严重破坏,因此可以定义NOMINMAX宏以防止它们被定义。

As a rule, if you're using C++ (as opposed to C) and including windows.h, alwaysdefine NOMINMAXfirst.

通常,如果您使用 C++(而不是 C)并包含windows.h,请始终NOMINMAX先定义。

回答by Dave S

If you're on VC++, you can either use #define NOMINMAXprior to including any headers, or do (std::max)(myInt + 2 * border, myOtherInt + 2 * border)

如果您使用的是 VC++,则可以#define NOMINMAX在包含任何标头之前使用,或者执行(std::max)(myInt + 2 * border, myOtherInt + 2 * border)

回答by Jim Buck

I would say that either maxis #define's to something else or you need to explicitly invoke the template via std::max<int>.

我会说要么max#define对别的东西,要么你需要通过std::max<int>.

回答by PeterN

The "using" declaration (see using Declaration) is yet another way to work around the issue:

“using”声明(参见using Declaration)是另一种解决问题的方法:

int border = 35;
int myInt = 2;
int myOtherInt = 3;
using std::max;
int z = max(myInt + 2 * border, myOtherInt + 2 * border);

It allows using std::max without explicit qualification.

它允许在没有明确限定的情况下使用 std::max。

回答by jweyrich

Have you tried using ::std::maxinstead? If this doesn't work, something is messing with your std namespace.

您是否尝试过使用::std::max?如果这不起作用,则说明您的 std 命名空间有问题。