| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 486 人关注过本帖
标题:[求助] 请问这个模板函数哪里出问题了?
只看楼主 加入收藏
gao_gao86
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2007-3-3
收藏
 问题点数:0 回复次数:2 
[求助] 请问这个模板函数哪里出问题了?

#include <iostream>
#include <cstring>
using namespace std;

template<typename Type>
Type max(Type T1,Type T2)
{
return (T1>T2 ? T1:T2);
}

typedef const char* PCC;

template<> PCC max<PCC>(PCC s1,PCC s2)
{
return (strcmp(s1,s2)>0 ? s1:s2);
}


int main()
{
int i=max(0,5);
const char *p=max("hello","world");
cout<<"i: "<<i<<endl;
cout<<"p: "<<p<<endl;
return 0;
}

编译器报错;
d:\c++\exercise 10_8\exercise 10_8.cpp(22) : error C2668: 'max' : ambiguous call to overloaded function
d:\c++\exercise 10_8\exercise 10_8.cpp(7): could be 'Type max<int>(Type,Type)'
with
[
Type=int
]
e:\program files\microsoft visual studio 8\vc\include\xutility(2936): or 'const _Ty &std::max<int>(const _Ty &,const _Ty &)'
with
[
_Ty=int
]
while trying to match the argument list '(int, int)'
d:\c++\exercise 10_8\exercise 10_8.cpp(23) : error C2668: 'max' : ambiguous call to overloaded function
d:\c++\exercise 10_8\exercise 10_8.cpp(14): could be 'PCC max<PCC>(PCC,PCC)'
e:\program files\microsoft visual studio 8\vc\include\xutility(2936): or '_Ty (&std::max<const char[6]>(_Ty (&),_Ty (&)))'
with
[
_Ty=const char [6]
]
while trying to match the argument list '(const char [6], const char [6])'
生成日志保存在“file://d:\C++\exercise 10_8\Debug\BuildLog.htm”
exercise 10_8 - 2 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========


我不知道哪里错了,希望高手指教,谢谢啊!

搜索更多相关主题的帖子: 函数 模板 
2007-05-13 18:48
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 

#include <iostream>
#include <cstring>
using namespace std;

template<typename T> T *max(T *s1,T *s2)
{
return (strcmp(s1,s2)>0 ? s1:s2);
}

int main()
{
int i=max(0,5);
const char *p=max("hello","world");
cout<<"i: "<<i<<endl;
cout<<"p: "<<p<<endl;
return 0;
}


=×&D o I p R e E n C g T l X&×=
2007-05-13 21:45
gao_gao86
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2007-3-3
收藏
得分:0 
回复:(踏魔狼)#include #include ...

如果我是用模板的显式特化而不是重新定义一个新的模板呢,为什么会报错呢?

2007-05-14 19:59
快速回复:[求助] 请问这个模板函数哪里出问题了?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014597 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved