2012-10-06 96 views
0

我收到一個錯誤,說'T'沒有命名一個類型。我對這意味着什麼感到困惑。我以爲我在課堂上宣稱虛擬T?類型沒有聲明錯誤

template <class T> 
class ABList : public ABCList<T> { 
private: 
    T a [LIST_MAX]; 
    int size; 

public: 
     ABList(); 

    virtual bool isEmpty(); 
    virtual int getLength(); 
    virtual void insert (int pos, T item); 
    virtual T remove (int pos); 
    virtual T retrieve (int pos); 
}; 

T ABList::retrieve (int pos) throw (ListException) 
{ 
    if (pos <= 0 || pos >= count) 
     throw new ListException(); 
    return item[pos – 1]; 
} 

回答

2

你必須寫爲:

template<typename T> 
T ABList<T>::retrieve (int pos) throw (ListException) 
{ 
    //... 
} 

因爲ABList類模板。

請注意,您必須在定義類模板的同一文件本身中定義成員函數。在.h文件中定義類模板,.cpp中的成員函數而不是在模板的情況下工作。

+0

啊所以只是添加模板 mystycs

+0

@soniccool:另外,'ABList ::'而不是'ABList ::'。 – Nawaz

+0

是的,我剛剛得到了哈哈謝謝! – mystycs