2016-01-22 151 views
1

嗨,我仍然不知道爲什麼我收到此錯誤信息:使用類模板需要模板參數

類模板「陣」的使用需要模板參數

標題:

#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 

CPP文件

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引用對象。

謝謝。編譯器看到Array<T>::

回答

5

之前,它不知道你所定義的類模板的成員,因此,你不能使用注射類名Array速記爲Array<T>。你需要寫const Array<T> &

而你在你的賦值操作符中向後得到了const。它應該接受一個const引用並返回一個非const的引用。

另外,Why can templates only be implemented in the header file?

相關問題