std::min 与std::max 的 Compiler Error C2780

代码

#include<iostream>
#include <algorithm> // std::min
#undef min
int main()
{
float a =15.0f;
float c ;
c=std::min(10,a);
printf("%f",b);
getchar();
}

错误提示

Error 1 error C2780: 'const _Ty &std::min(const _Ty &,const _Ty &,_Pr)' : expects 3 arguments - 2 provided 
Error 2 error C2782: 'const _Ty &std::min(const _Ty &,const _Ty &)' : template parameter '_Ty' is ambiguous
3 IntelliSense: no instance of overloaded function "std::min" matches the argument list
4 IntelliSense: too few arguments in function call

unction template (C++98)

template <class T> const T& min (const T& a, const T& b) {
  return !(b<a)?a:b;     // or: return !comp(b,a)?a:b; for version (2)
}
错误分析
min采用的stl模板,算法的原型中,a,b两个形参应该为相同类型,c=std::min(10,a); 10 与 a 的类型不匹配导致了一下的错误

修改方法
方法1
c=std::min(10.0f,a);
方法2
float b=10;
c=std::min(b,a);
方法3
c=std::min((float)10.0,a);

 

 
posted @ 2014-09-21 18:13  射手炎魂  阅读(1353)  评论(0编辑  收藏  举报