| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1009 人关注过本帖
标题:[求助]函数模板具体化的问题
只看楼主 加入收藏
触电
Rank: 1
等 级:新手上路
威 望:1
帖 子:228
专家分:0
注 册:2006-7-26
收藏
 问题点数:0 回复次数:12 
[求助]函数模板具体化的问题

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

template<typename T>
T Max(const T str[],const int n);

template<>const char* Max(const char **str,const int n);

int main()
{
int str[]={3,2,10,6,8};
int max1=Max(str,5);
cout<<max1<<endl;

double array[]={1.23,4.56,13.56,2.87,8.76};
double max2=Max(array,5);
cout<<max2<<endl;

char *shuzu[]={"linking Park","bsb","nsync"};
char *max3=Max(shuzu,3);
cout<<max3<<endl;

system("pause");
return 0;
}

template<typename T>
T Max(const T str[],const int n)
{
T max=str[0];
int i;
for(i=1;i<5;i++)
if(max<str[i])
max=str[i];

return max;
}


template<>const char* Max(const char **str,const int n)
{
const char *max=str[0];
int i;
for(i=1;i<n;i++)
if(strlen(str[i])>strlen(max))
max=str[i];

return max;
}


--------------------Configuration: paixu - Win32 Debug--------------------
Compiling...
paixu.cpp
C:\Program Files\Microsoft Visual Studio\程序\paixu.cpp(8) : error C2912: explicit specialization; 'const char *__cdecl Max(const char *[],const int)' is not a function template
C:\Program Files\Microsoft Visual Studio\程序\paixu.cpp(8) : see declaration of 'Max'
C:\Program Files\Microsoft Visual Studio\程序\paixu.cpp(41) : error C2912: explicit specialization; 'const char *__cdecl Max(const char *[],const int)' is not a function template
C:\Program Files\Microsoft Visual Studio\程序\paixu.cpp(8) : see declaration of 'Max'
C:\Program Files\Microsoft Visual Studio\程序\paixu.cpp(41) : error C2912: explicit specialization; 'const char *__cdecl Max(const char *[],const int)' is not a function template
C:\Program Files\Microsoft Visual Studio\程序\paixu.cpp(8) : see declaration of 'Max'


有点晕,请指教,谢谢

搜索更多相关主题的帖子: 函数 模板 具体化 
2006-08-14 21:14
触电
Rank: 1
等 级:新手上路
威 望:1
帖 子:228
专家分:0
注 册:2006-7-26
收藏
得分:0 

顶一下

[此贴子已经被作者于2006-8-14 21:21:35编辑过]

2006-08-14 21:18
触电
Rank: 1
等 级:新手上路
威 望:1
帖 子:228
专家分:0
注 册:2006-7-26
收藏
得分:0 
各位兄台,过来看看吧
2006-08-14 21:46
wangxiang
Rank: 2
等 级:新手上路
威 望:5
帖 子:376
专家分:0
注 册:2006-3-28
收藏
得分:0 

你的摸板写的不大好
不要乱用const,以下没有改摸板,只是去掉了几个const
#include<iostream>
#include<cstring>
using namespace std;

template<typename T>
T Max(const T str[],const int n);

char* Max( char **str,const int n);

int main()
{
int str[]={3,2,10,6,8};
int max1=Max(str,5);
cout<<max1<<endl;

double array[]={1.23,4.56,13.56,2.87,8.76};
double max2=Max(array,5);
cout<<max2<<endl;

char *shuzu[]={"linking Park","bsb","nsync"};
char *max3=Max(shuzu,3);
cout<<max3<<endl;

system("pause");
return 0;
}

template<typename T>
T Max(const T str[],const int n)
{
T max=str[0];
int i;
for(i=1;i<5;i++)
if(max<str[i])
max=str[i];

return max;
}


char* Max(char **str,const int n)
{
char *max=str[0];
int i;
for(i=1;i<n;i++)
if(strlen(str[i])>strlen(max))
max=str[i];

return max;
}



2006-08-14 22:22
触电
Rank: 1
等 级:新手上路
威 望:1
帖 子:228
专家分:0
注 册:2006-7-26
收藏
得分:0 
你把我的第二个显示具体化都改了,呵呵
我就是想用显示具体化来生成函数啊!
还是谢了...
2006-08-14 22:31
wangxiang
Rank: 2
等 级:新手上路
威 望:5
帖 子:376
专家分:0
注 册:2006-3-28
收藏
得分:0 

有你第二个的哪种模板吗?


2006-08-14 22:39
wangxiang
Rank: 2
等 级:新手上路
威 望:5
帖 子:376
专家分:0
注 册:2006-3-28
收藏
得分:0 
你的第二个类型早已具体化了char*

2006-08-14 22:41
触电
Rank: 1
等 级:新手上路
威 望:1
帖 子:228
专家分:0
注 册:2006-7-26
收藏
得分:0 
1.对于给定的函数名,可以有非模板函数模板函数和显示具体化函数以及它们的重载版本
2.显示具体化的原形和定义应以template<>打头,并通过名称来指出其类型
3.具体化将覆盖常规模板,而非模板函数将覆盖具体化和常规模板

请指教
2006-08-14 22:46
触电
Rank: 1
等 级:新手上路
威 望:1
帖 子:228
专家分:0
注 册:2006-7-26
收藏
得分:0 
晕..你好快
2006-08-14 22:46
woodhead
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:1124
专家分:0
注 册:2005-7-18
收藏
得分:0 
template <class T>bool less(T a, T b)
{
return a < b;
}

template<>bool less ( const char* a, const char* b)
{
return strcmp(a, b);
}

我想这里 const char* 对应 T 的类型。const也是类型的一部分。
const T 应该被看成是什么,我也不太清楚。

忘写返回了

[此贴子已经被作者于2006-8-14 23:18:20编辑过]


2006-08-14 23:16
快速回复:[求助]函数模板具体化的问题
数据加载中...
 
   



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

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