错误代码:
#include<iostream>
using namespace std;
template <class T>
T min(T x,T y){
if(x<y){
return x;
}
else return y;
}
int main(){
int n1=2,n2=10;
double d1=1.5,d2=5.6;
cout<<min(n1,n2)<<endl;
cout<<min(d1,d2)<<endl;
}
正确代码:
#include<iostream>
using namespace std;
template <class T>
T min(T x,T y){
if(x<y){
return x;
}
else return y;
}
int main(){
int n1=2,n2=10;
double d1=1.5,d2=5.6;
cout<<::min(n1,n2)<<endl;
cout<<::min(d1,d2)<<endl;
}
分析:
我定义的min与命名空间冲突了,所以报错
解决:
::min();//调用的自己写的min()
std::min();//调用的标准模板库的min