注册 登录
编程论坛 C++教室

[求助]请改正下面的简单的函数模板, 让它正确运行。。。。谢谢

cpluslover 发布于 2007-04-23 19:14, 481 次点击

#include<iostream.h>

template〈class T〉
T max(T a, T b)
{
return a>b?a:b;
}

int main()
{
cout<<"the max of 2 and 7 is "<< max(2, 7)<<endl;
cout<<"the max of a and c is "<< max('a','c')<<endl;

return 0;
}

[此贴子已经被作者于2007-4-23 19:18:16编辑过]

4 回复
#2
aipb20072007-04-23 19:52
你用的什么编译器?


我用VC编译正确,用dec-cpp编译提示错误,原因是,在std空间里定义了一个名为max的函数,所以够成不明确的调用!

不知道为什么!

[此贴子已经被作者于2007-4-23 19:57:38编辑过]

#3
aipb20072007-04-23 20:05
还有我想你是不是自己的问题啊,我发现你<>打错了

template〈class T〉

应该是template<class T>
或许你发现的问题不是我那个,呵呵!
#4
cpluslover2007-04-23 20:43
明白了谢谢呵呵
#5
chenkuanyi2007-04-23 22:09

#include<iostream>
using namespace std;

template<class T>
T max(T& a, T& b)
{
return a>b?a:b;
};

int main()
{
cout<<"the max of 2 and 7 is "<< max(2, 7)<<endl;
cout<<"the max of a and c is "<< max('a','c')<<endl;

return 0;
}

the max of 2 and 7 is 7
the max of a and c is c

1