帮忙看一下这个程序为什么错?

来源:百度知道 编辑:UC知道 时间:2024/06/13 22:07:08
#include "stdafx.h"
#include <iostream>
using namespace std;
template<typename T>
T max(T a,T b)
{
return(a>b?a:b);
}

int _tmain(int argc, _TCHAR* argv[])
{ double c;
c=max(2.3,4.3);
cout<<c<<endl;
return 0;
}
这是在visual 2005下编写的
错误也附上
1>g:\编程c++\week2\week2\week2.cpp(16) : error C2668: 'max' : ambiguous call to overloaded function
1> g:\编程c++\week2\week2\week2.cpp(8): could be 'T max<double>(T,T)'
1> with
1> [
1> T=double
1> ]
1> e:\visual studio2005\vc\include\xutility(2936): or 'const _Ty &std::max<double>(const _Ty &,const _Ty &)'
1> with
1> [
1> _Ty=double
1> ]
1> while trying to match the argument list '(double, double)'

#include <iostream>

using namespace std;

template<typename T>
T my_max(T a,T b)
{
return(a>b?a:b);
}

int main(int argc, char * argv[])
{
double c;
c = my_max((float)2.3, (float)4.3);
cout<< c << endl;
return 0;
}

/*
我认为你的max函数与C++库中的max函数存在重载二义性
我的编译环境
gcc (GCC) 4.1.0 (SUSE Linux)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#include <iostream>
using namespace std;
template<typename T>
T max(T a,T b)
{
return(a>b?a:b);
}

int main(int argc, char* argv[])
{
double c;
c=max(2.3,4.3);
cout<<c<<endl;
return 0;
}

vc6下编译通过。

我用的VS6.0