你好,我仍然在想为什么会得到以下错误信息:
使用类模板“Array”需要模板参数。
头文件:
#ifndef Array_h
#define Array_h
template< typename T>
class Array
{
public:
Array(int = 5);
Array( const Array &);
const Array &operator=(Array &);
T &operator[](int);
T operator[](int) const;
// getters and setters
int getSize() const;
void setSize(int);
~Array();
private:
int size;
T *ptr;
bool checkRange(int);
};
#endif
C++文件
template< typename T >
const Array &Array< T >::operator=(Array &other)
{
if( &other != this)
{
if( other.getSize != this->size)
{
delete [] ptr;
size = other.getSize;
ptr = new T[size];
}
for ( int i = 0; i < size; i++)
{
ptr[i] = other[i];
}
}
return *this;
}
问题似乎出在返回对象的const引用上。谢谢。