std::min and std::max 出错解决方案
来源:互联网 发布:黑手党知乎 编辑:程序博客网 时间:2024/05/16 11:23
C++的std::min and std::max 如:
float_t f(float_t x) const { return std::max((float_t)0.0, x); }
但是使用时如下代码:
#include "windows.h"#include <algorithm>void foo() { int i = 5; int j = 7; int x = std::max(i,j);}
在VS中编译可能会出现以下错误:
1>test.cpp(7) : error C2589: '(' : illegal token on right side of '::'1>test.cpp(7) : error C2143: syntax error : missing ';' before '::'
这是因为windows.h
中也定义了max和min宏,当你包含了windows.h
时,该程序就不能通过编译。
解决方案:
1. windows.h使用另外一个名字,只适用于windows系统。
int x = _cpp_max(i,j);int y = _cpp_min(i,j);
该方法在系统库上改动,不推荐。
2. 不使用系统库中的std::max()
和std::min()
函数,用其他代替如:
int x = i > j ? i : j; // max(i,j)int y = i < j ? i : j; // min(i,j)
简单地重新实现这些函数,如果代码量大,可自己重新写一个不同名称的函数。
3. 添加命名空间using namespace std;
或如下:
using std::min;using std::max;int x = max(i,j);int y = min(i,j);
该方法实验不行,不知是不是我理解错了,欢迎讨论。
4. 使用 std::min<int>
and std::max<int>
如
int x = std::max<int>(i,j);int y = std::min<int>(i,j);
该方法需要事先知道输入数据类型,但对于i和j类型不同时会出现问题,如:
int i = 5;unsigned int j = 7;int x = (std::max)(i,j);int y = (std::min)(i,j);
会出现以下错误:
1>test.cpp(7) : error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' :expects 3 arguments - 2 provided1> c:program filesmicrosoft visual studio 8vcincludexutility(3190) :see declaration of 'std::max'1>test.cpp(7) : error C2782: 'const _Ty &std::max(const _Ty &,const _Ty &)' :template parameter '_Ty' is ambiguous1> c:program filesmicrosoft visual studio 8vcincludexutility(3182) :see declaration of 'std::max'1> could be 'unsigned int'1> or 'int'
总结:推荐方法2,自己实现一个相同功能的函数。
0 0
- std::min and std::max 出错解决方案
- std::min and std::max
- std::min and std::max
- std::min and std::max
- std::min and std::max
- std::min and std::max
- std::min std::max
- 使用std:min(),std:max()
- std::max, std::min错误
- std::min and std::max与windows.h定义的min和max宏相冲突.
- Windows下编译std::numeric_limits::max() 和std::numeric_limits::min()出错
- std::min,std::max 与 MFC冲突的解决
- std::min&std::max与mfc冲突问题
- std::numeric_limits::max() std::numeric_limits::min()编译错误
- std::numeric_limits::max() std::numeric_limits::min()编译错误
- std::numeric_limits::max() std::numeric_limits::min()编译错误
- std::min, std::max, std::swap, std::rel_ops的简单使用
- 标准库std::min/std::max与min/max宏冲突
- HDFS简介
- springBoot tomcat启动
- mybatis介绍
- 记忆化搜索,状压DP(20个问题,uva 1252)
- CAPropertyAnimation 用法
- std::min and std::max 出错解决方案
- javascript的基础
- 面向对象(补充)
- js写快速排序
- Tomcat9.0启动tomcat unsupported major.minor version 52.0异常
- 数据仓库相关术语
- 关联CMS管理的数据和静态页面显示的数据
- javascript应用--1
- 二分法的适用范围及通用代码