主题:[讨论]一直有三个错误,求助求助
#include<iostream>
using namespace std;
template<class ElemType, int size>
class Array
{
private:
ElemType A[size] ;
public:
template<class ElemType, int size>
void setA(ElemType E[],int m)
{ int i;
for( i=0;i<m;i++)
{A[i]=E[i];}
for( i=0;i<m;i++)
cout<<A[i]<<" "; cout<<endl; }
template<class ElemType, int size>
ElemType Max(ElemType A[ ], int m)
{
ElemType t=A[0];
for(int i=1;i<m;i++)
if(t<A[i]) t=A[i];
return t;
}
template<class ElemType, int size>
ElemType Sum(ElemType A[], int m)
{
ElemType sum=0;
for(int i=0;i<m;i++)
{sum+=A[i];}
return sum;
}
};
int main( )
{
int a[ ]={1,2,3,4,5,6};
const int n=6;
Array<int , 6> A;
A.setA(a ,6);
cout<<"数组最大元素为:"<<A.Max(a, 6)<<endl;
cout<<"数组元素之和为:"<<A.Sum(a, 6)<<endl;
system("PAUSE");
return 0;
}
using namespace std;
template<class ElemType, int size>
class Array
{
private:
ElemType A[size] ;
public:
template<class ElemType, int size>
void setA(ElemType E[],int m)
{ int i;
for( i=0;i<m;i++)
{A[i]=E[i];}
for( i=0;i<m;i++)
cout<<A[i]<<" "; cout<<endl; }
template<class ElemType, int size>
ElemType Max(ElemType A[ ], int m)
{
ElemType t=A[0];
for(int i=1;i<m;i++)
if(t<A[i]) t=A[i];
return t;
}
template<class ElemType, int size>
ElemType Sum(ElemType A[], int m)
{
ElemType sum=0;
for(int i=0;i<m;i++)
{sum+=A[i];}
return sum;
}
};
int main( )
{
int a[ ]={1,2,3,4,5,6};
const int n=6;
Array<int , 6> A;
A.setA(a ,6);
cout<<"数组最大元素为:"<<A.Max(a, 6)<<endl;
cout<<"数组元素之和为:"<<A.Sum(a, 6)<<endl;
system("PAUSE");
return 0;
}