
c++中的函数模板
#include<iostream>template<classT>TMAX(T*P);intmain(){usingnamespacestd;intsh[5];inti...
#include<iostream>
template<class T>
T MAX(T *P);
int main()
{using namespace std;
int sh[5];
int i=0;
double ch[5];
cout<<"enter 5 int num"<<endl;
for( i=0;i<5;i++)
cin>>sh[i];
T max=MAX(sh);
cout<<max;
cout<<"enter 5 double num"<<endl;
for( i=0;i<5;i++)
cin>>ch[i];
T max=MAX(ch);
cout<<max;
return 0;
}
template <class T>
T MAX(T *P)
{T tem;
for(int i=0;i<5;i++)
if(tem<p[i])
tem=p[i];
return tem;
} 展开
template<class T>
T MAX(T *P);
int main()
{using namespace std;
int sh[5];
int i=0;
double ch[5];
cout<<"enter 5 int num"<<endl;
for( i=0;i<5;i++)
cin>>sh[i];
T max=MAX(sh);
cout<<max;
cout<<"enter 5 double num"<<endl;
for( i=0;i<5;i++)
cin>>ch[i];
T max=MAX(ch);
cout<<max;
return 0;
}
template <class T>
T MAX(T *P)
{T tem;
for(int i=0;i<5;i++)
if(tem<p[i])
tem=p[i];
return tem;
} 展开
展开全部
//以下是正确程序,自己找自己错在哪里
#include <iostream>
using namespace std;
template<class T>
T MAX(T *P);
int main()
{
int sh[5];
int i=0;
double ch[5];
cout<<"enter 5 int num"<<endl;
for( i=0; i<5; i++)
cin>>sh[i];
int max=MAX(sh);
cout<<max<<endl;
cout<<"enter 5 double num"<<endl;
for( i=0; i<5; i++)
cin>>ch[i];
double max2=MAX(ch);
cout<<max2<<endl;
return 0;
}
template <class T>
T MAX(T *p)
{
T tem = p[0];
for(int i=1; i<5; i++)
if(tem<p[i])
tem=p[i];
return tem;
}
#include <iostream>
using namespace std;
template<class T>
T MAX(T *P);
int main()
{
int sh[5];
int i=0;
double ch[5];
cout<<"enter 5 int num"<<endl;
for( i=0; i<5; i++)
cin>>sh[i];
int max=MAX(sh);
cout<<max<<endl;
cout<<"enter 5 double num"<<endl;
for( i=0; i<5; i++)
cin>>ch[i];
double max2=MAX(ch);
cout<<max2<<endl;
return 0;
}
template <class T>
T MAX(T *p)
{
T tem = p[0];
for(int i=1; i<5; i++)
if(tem<p[i])
tem=p[i];
return tem;
}
更多追问追答
追问
也就是说主函数里面不能用T来定义变量。但是你这个程序好想还有问题啊
追答
我用g++可以编译运行是正确的,你说有什么问题?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询