C++ 代码6.0通过 2005不行,请问怎么修改

来源:百度知道 编辑:UC知道 时间:2024/06/17 16:22:35
VC 6.0下编译通过,但是到2005下发生
错误 1 error C2668: “max”: 对重载函数的调用不明确

代码如下:

#include <iostream>
using namespace std;

template <typename T>
T max (T a,T b)
{
return ((a>b)?a:b);
}
void main()
{
double x,y;
cin>>x>>y;
cout<<"Max number is "<<(max(x,y))<<endl;
cin>>x;
}

请问如何修改?

名字冲突,std中已经有名为max的函数了。

#include <iostream>

template <typename T>
T max (T a,T b)
{
return ((a>b)?a:b);
}
int main()
{
double x,y;
std::cin>>x>>y;
std::cout<<"Max number is "<<(max(x,y))<<std::endl;
std::cin>>x;
}



#include <iostream>
using namespace std;

template <typename T>
T my_max (T a,T b)
{
return ((a>b)?a:b);
}
int main()
{
double x,y;
cin>>x>>y;
cout<<"Max number is "<<(my_max(x,y))<<endl;
cin>>x;
}