I have following code:
#include <iostream>
using namespace std;
template<class T>
T max(T *data,int len){
int i;
T Max=data[0];
for (int i=1;i<len;i++){
if (data[i]>max){
Max=data[i];
}
}
return Max;
}
int mai(){
int i[]={12,34,10,9,56,78,30};
int len=sizeof(i)/sizeof(i[0]);
cout<<max(i,len)<<"
";
return 0;
}
But when I compile it, I get the following errors:
generic_max, Configuration: Debug Win32 ------
1>Build started 7/29/2010 1:03:25 AM.
1>InitializeBuildStatus:
1> Touching "Debuggeneric_max.unsuccessfulbuild".
1>ClCompile:
1> generic_max.cpp
1>c:usersdaviddocumentsvisual studio 2010projectsgeneric_maxgeneric_maxgeneric_max.cpp(10): error C2563: mismatch in formal parameter list
1> c:usersdaviddocumentsvisual studio 2010projectsgeneric_maxgeneric_maxgeneric_max.cpp(22) : see reference to function template instantiation 'T max<int>(T *,int)' being compiled
1> with
1> [
1> T=int
1> ]
1>c:usersdaviddocumentsvisual studio 2010projectsgeneric_maxgeneric_maxgeneric_max.cpp(10): error C2568: '>' : unable to resolve function overload
1> c:usersdaviddocumentsvisual studio 2010projectsgeneric_maxgeneric_maxgeneric_max.cpp(4): could be 'T max(T *,int)'
1> c:program filesmicrosoft visual studio 10.0vcincludexutility(2086): or 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)'
1> c:program filesmicrosoft visual studio 10.0vcincludexutility(2078): or 'const _Ty &std::max(const _Ty &,const _Ty &)'
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.95
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Please help me to fix this problem.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…