std::max、std::min error C2589: “(”:“::”右边的非法标记,error C2059: 语法错误:“::”

在VC++种同时包含头文件#include <windows.h>和#include <algorithm>后就会出现无法正常使用std标准库中的min和max模板函数,经过查阅发现这是因为在Windows.h种也有min和max的定义,这样就导致了algorithm中的min和max无法正常使用,这里给出两种解决方案,来解决std命名空间无法使用min和max的问题。

解决方案一【推荐

使用std::min或者std::max的时候加上括号,避免与Windows.h中的min、max宏定义冲突。

1

2

3

4

#include <windows.h>

#include <algorithm>

(std::min)(100,2000);

(std::max)(10,500);

ui->lineEdit_workpiece_spacing->setValidator(new QDoubleValidator(0.0, (std::numeric_limits<double>::max)(), 5, this));

 

解决方案二【不推荐

禁用Windows.h中的min、max宏定义。

在Windows.h中可以查阅到min、max的定义为:

01

02

03

04

05

06

07

08

09

10

11

#ifndef NOMINMAX

 

#ifndef max

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

#endif

 

#ifndef min

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

#endif

 

#endif  /* NOMINMAX */

看懂了定义就很简单了,在包含Windows.h文件之前直接定义一个NOMINMAX宏定义就OK了,如下代码所示:

1

2

3

4

#define NOMINMAX

#include <windows.h>

#include <algorithm>

std::max(100, 200);

OK,经过上面的操作,min,max操作已经正常了。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值