那位高手帮忙看看这段程序中的模板具体化函数什么错误,老是不通过编译?多谢

C、C++和Java语言
回复
dyfbb
帖子: 13
注册时间: 2007-09-11 18:07
送出感谢: 0
接收感谢: 0

那位高手帮忙看看这段程序中的模板具体化函数什么错误,老是不通过编译?多谢

#1

帖子 dyfbb » 2007-10-14 16:34

#include <iostream>
#include <cstring>

using namespace std;

template <typename T>
T maxn(const T arr[],int n);
template <> char * maxn<char *>(const char * ch[],int n);

int main()
{
int iarr[6]={101,202,105,403,650,330};
long larr[4]={11L,22L,33L,23L};
cout<<maxn(iarr,6)<<endl;
cout<<maxn(larr,4)<<endl;
char * ch[5]={"qaz","wsxe","qwer","rt","rtfx"};
cout<<maxn(ch,5);
return 0;
}

template <typename T>
T maxn(const T arr[],int n)
{
T tmp=arr[0];
for (int i=1;i<n;i++)
if (tmp<arr)
tmp=arr;
return tmp;
}

template <> char * maxn<char *>(const char * ch[],int n)
{
int ilenmax=strlen(*ch[0]),ilen;
char * pch= ch[0];

for (int i=1;i<n;i++)
{
ilen = strlen(*ch);
if (ilenmax<ilen)
{
ilenmax=ilen;
pch = ch;
}
}

return pch;

}
minibox
帖子: 12
注册时间: 2007-10-20 9:09
送出感谢: 0
接收感谢: 0

#2

帖子 minibox » 2007-10-21 23:12

把main函数移到最后看看
cg19820811
帖子: 40
注册时间: 2007-04-09 15:30
送出感谢: 0
接收感谢: 0
联系:

#3

帖子 cg19820811 » 2007-10-22 11:27

#include <iostream>
#include <cstring>

using namespace std;

template <typename T>
T maxn(const T arr[],int n);

template <typename T> char * maxn<char *>(const char * ch[],int n);

int main()
{
int iarr[6]={101,202,105,403,650,330};
long larr[4]={11L,22L,33L,23L};
cout<<maxn(iarr,6)<<endl;
cout<<maxn(larr,4)<<endl;
char * ch[5]={"qaz","wsxe","qwer","rt","rtfx"};
cout<<maxn(ch,5);
return 0;
}

template <typename T>
T maxn(const T arr[],int n)
{
T tmp=arr[0];
for (int i=1;i<n;i++)
if (tmp<arr)
tmp=arr;
return tmp;
}

template <typename T>
char * maxn<char *>(const char * ch[],int n)
{
int ilenmax=strlen(*ch[0]),ilen;
char * pch= ch[0];

for (int i=1;i<n;i++)
{
ilen = strlen(*ch);
if (ilenmax<ilen)
{
ilenmax=ilen;
pch = ch;
}
}

return pch;

}
回复

回到 “C/C++/Java”