请大家帮忙修改一下代码
array.h中
//抽象数组的声明
#ifndef ARRAY
#define ARRAY
#include<iostream>
using namespace std;
template <class T> class Array
{
public:
Array(int size); //构造函数,默认构造十个元素的数组
Array(const Array<T> &); // 复制构造函数
~Array() {delete element;} //析构函数
int Length()const {return size;} //获取数组大小
T& operator [](int i) const; //用下标访问数组元素
Array<T> & operator =(const Array<T> &); //复制数组
void Resize(int i); //重置数组大小
private:
T *element;
int size;
};
#endif
array.cpp中
// 抽象数组的实现
#include "array.h"
// 构造函数
template <class T> Array<T>::Array(int sz=10)
{
if (sz<=0){cerr<<"invalid size"<<endl;return;}
element=new T[sz];
if(NULL!=element)
size=sz;
else
{
cerr<<"memory allocation error"<<endl;
return;
}
}
//复制构造函数
template<class T> Array<T>::Array(const Array<T> & a)
{
size=a.size;
element=new T[size];
if(NULL==element)
{
cerr<<"memory allocation error"<<endl;
return;
}
for(int i=0;i!=size;i++)
{
element[i]=a[i];
}
}
//重载下标访问运算符
template<class T> T& Array<T>::operator [](int i) const
{
return element[i];
}
//复制数组
template<class T> Array<T>& Array<T>::operator =(const Array<T> &a)
{
if(size!=a.size)
{
size=a.size;
delete[] element;
element=new T[size];
if(NULL==element)
{
cerr<<"memory allocation error"<<endl;
return;//**********
}
}
for(int i=0;i!=size;i++)
{
element[i]=a[i];
}
return *this;
}
//重置数组大小
template<class T> void Array<T>:: Resize(int i)
{
if(size!=i)
{
T* NewElement=new T[i];
if(NULL==NewElement)
{
cerr<<""<<endl;
return;
}
int j=i<size? i:size;
while(j--) NewElement[j]=element[j];
delete[] element;
element=NewElement;
size=i;
}
}
main.cpp中
#include"array.h"
#include<iostream>
using namespace std;
int main()
{
Array <char> MyArray(20);
cout<<MyArray.Length()<<endl;
system("pause");
return 0;
}
上面程序编译提示 : [Linker error] undefined reference to `Array<char>::Array(int)
另外大家觉得哪里有不合理的地方,可有更好的实现也尽量说出来,不胜感激