高手帮忙看看,为为什么这个数组模板类不行啊
template<class T,class T1>
class myArray
{
public:
myArray(int size);
//~myArray();
T1 Sum(void);
T1 average_value(void);
void show_array(void);
add_value(T);
private:
T * data;
int size;
int index;
};
////////
template<class T,class T1>
myArray<T,T1>::myArray(int size)
{
size=size;
index=0;
data=new T[size];
if(data==NULL)
{
cerr<<"Insufficent memory!";
exit(1);
}
}
/*
template<class T,class T1>
myArray<T,T1>::~myArray()
{
delete[] data;
}*/
template<class T,class T1>
T1 myArray<T,T1>::Sum(void)
{
T1 sum=0;
for(int i=0;i<index;i++)
sum=sum+data[index];
return sum;
}
template <class T,class T1>
T1 myArray<T,T1>::average_value(void)
{
T1 sum=0;
for(int i=0;i<index;i++)
sum=sum+data[index];
return sum/index;
}
template<class T,class T1>
void myArray<T,T1>::show_array(void)
{
for(int i=0;i<index<i++)
{
cout<<data[index]<<" ";
cout<<endl;
}
}
template <class T,class T1>
myArray<T,T1>::add_value(T t)
{
if(index==size)
return -1;
data[index++]=t;
}
/////
#include "TemplateClass.h"
int main(int argc, char* argv[])
{
myArray<int,long> number(20);
for(int i=0;i<20;i++)
{
number.add_value(i);
}
number.show_myArray();
return 0;
}