#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'
有点晕,请指教,谢谢