2012-10-08 45 views
0

我一直在爲我的某個函數獲取編譯器錯誤。C++:關於鏈表的實現

LinkedList.hpp:81: error: `template<class T> class LinkedList' used without template parameters 
LinkedList.hpp:81: error: expected constructor, destructor, or type conversion before '*' token 
LinkedList.hpp:81: error: expected `;' before '*' token 

但事情是我有一個構造函數,析構函數和類型轉換。我敢肯定的是,執行是錯誤的

// This is the function i keep on getting an error for 
template <class T> 
ListNode* LinkedList<T>::find(int pos)//Finds the position of an item 
{ 
    if(pos < 1) 
     return NULL; //If pos is less than one then find returns NULL because pos is a illegal value. 
    else 
    { 
     ListNode *temp = head; 
     for(int i = 1; i < pos; i++) 
      temp = temp -> next; 
     return temp; 
    } 
} 

//The class 
template <class T> 
class LinkedList : public ABCList<T> { 
private: 
    //T a [LIST_MAX]; 

    struct ListNode 
    { 
     T data; // List item 
     ListNode *next; //Pointer to next node 
    }; 

    int size; 
    ListNode *head; 
    ListNode *find(int pos); 

public: 
    LinkedList(); 
    LinkedList(LinkedList &other); 
    ~LinkedList(); 
    virtual bool isEmpty() = 0; 
    virtual int getLength() = 0; 
    virtual void insert (int pos, T item) = 0; 
    virtual T remove (int pos) = 0; 
    virtual T retrieve (int pos) = 0; 
}; 
+2

哪裏代碼你在哪裏,你創建一個LinkedList對象? – imreal

+1

81線在哪裏? –

+0

線81'code'template ListNode * LinkedList的 ::發現(INT POS) –

回答

2
  1. 爲什麼當標準庫提供了一個創建鏈表? std::list是一個雙鏈表。
  2. 你能在find()定義改寫ListNode*typename LinkedList<T>::ListNode*
  3. 您必須選擇是否希望用戶能夠操縱ListNode,(在這種情況下,你應該把它聲明爲public),或者如果它是一部分的實現(在這種情況下,您可能想要創建某種類型的迭代器)。

我還是得到了同樣的錯誤

當時的find()定義位於LinkedList類的聲明之上,如問題提出?如果是這種情況,你應該換掉它們。

+0

我還是得到了同樣的錯誤 –

1

一兩件事,我看到的是,ListNode在LinkedList的定義,因此有資格這樣:

template <class T> 
typename LinkedList<T>::ListNode* LinkedList<T>::find(int pos) { 
    ... 
} 
+0

不過它不會工作怪異 –

+0

這裏的構造函數和析構函數和複製'code'Template LinkedList的 ::鏈表()//默認構造 { \t大小= 0; \t head = NULL; } 模板 鏈表 ::鏈表(鏈表和其他)//複製構造 { \t對(INT I = 1; I //拆解 鏈表 ::〜鏈表() { \t而(的isEmpty()!) \t \t刪除(1); }'code' –

+0

也許把它作爲你的問題的一部分?你可以編輯它,而其他人閱讀起來會更容易。 –