2012-12-24 48 views
0

試圖學習C++。想知道是否有人能指出我在這個錯誤的正確方向(雙關意圖)。C++:期望的構造函數,析構函數或類型轉換

該類是一個LinkedList(轉載於下面)。在這條線上出現的錯誤:

LinkedList<T>::ListNode LinkedList::find(int pos) const { 

,並說:

預期的構造函數,析構函數或類型轉換之前 '鏈表'

任何提示將不勝感激。

#ifndef LINKEDLIST_HPP 
#define LINKEDLIST_HPP 


#include <iostream> 

using namespace std; 

template <class T> 
class LinkedList { 

    private: 
     // a ListNode consists of the item and a pointer to the next ListNode 
     struct ListNode { 
      T data; 
      ListNode *next; 
     }; 
     int size; //Size of the list 
     ListNode *head; //the beginning of the list 

    public: 
     LinkedList(); // default oonstructor 
     ~LinkedList(); // destructor 
     virtual bool isEmpty(); 
     virtual int getLength(); 
     virtual void insert (int pos, T item); 
     virtual T remove (int pos); 
     virtual T retrieve (int pos); 

     //helper methods 
     LinkedList(LinkedList &copyList); //copy constructor 
     ListNode *find (int pos) const; // internal find function 
}; 

//default constructor 
template <class T> 
LinkedList<T>::LinkedList() { 
    size = 0; 
    head = NULL; 
} 

//destructor 
template <class T> 
LinkedList<T>::~LinkedList() { 
    //loop through each node, and delete it 
    ListNode* current = head; 
    while (current != NULL) { 
     ListNode* next = current->next; 
     delete current; 
     current = next; 
    } 
    head = NULL; // set head node to back to null 
} 

//copy constructor 
template <class T> 
LinkedList<T>::LinkedList(LinkedList& copyList) { 
    size = copyList.size; 

    // if copyList is empty 
    if (copyList.head == NULL) 
     head = NULL; 

    else { 
     //create a new head 
     head = new ListNode; 
     head->data = copyList.head->data; 

     //create a new list 
     ListNode *newPtr = head; // start at the head 
     // iterate through rest of list to be copied 
     for (ListNode *copyPtr = copyList.head->next; copyPtr != NULL; copyPtr = copyPtr->next) { 
      newPtr->next = new ListNode; 
      newPtr->data = copyPtr->data; 
     } 
     //make last ListNode's next point to NULL 
     newPtr->next = NULL; 
    } 
} 

template <class T> 
bool LinkedList<T>::isEmpty() { 
    if (size == 0) 
     return true; 
    return false; 
} 

template <class T> 
int LinkedList<T>::getLength() { 
    return size; 
} 

// used in other methods to find a given index 
template <class T> 
LinkedList<T>::ListNode LinkedList::find(int pos) const { 

    // check that pos is in bound of LinkedList 
    if ((pos < 1) || pos > getLength()) { 
     cout << "Find position of out bounds" << endl; 
     return NULL; 
    } else { //search through ListNodes 
     ListNode *temp = head; // start at the head 
     for (int i = 1; i < pos; ++i) 
      temp = temp->next; 
     return temp; 
    } 
} 


template <class T> 
T LinkedList<T>::retrieve(int pos) { 
    T tempData; // to hold retrieved data 
    try { 
     if ((pos < 1) || (pos > getLength())) { 
      cout << "Retrieve request outside LinkedList's bounds" << endl; 
      return NULL; 
     } 
     else { //traverse list 
      ListNode *temp = find(pos); 
      tempData = temp->data; 
      return tempData; 
     } 
    } catch (int e) { 
     cout << "Could not retrieve position " << pos << endl; 
    } 
} 

template <class T> 
void LinkedList<T>::insert(int pos, T item) { 

    //check bounds 
    if ((pos < 1) || (pos > getLength() +1)) 
     cout << "Must insert at a position between 1 and getLength() + 1" << endl; 

    else { 
     try { 
      //create new ListNode 
      ListNode *temp = new ListNode; 
      temp->data = item; 

      //if the new item is at the first position 
      if (pos == 1) { 
       temp->next = head; 
       head = temp; 
      } 
      else { 
       ListNode *prev = find(pos - 1); 
       temp->next = prev->next; 
       prev->next = temp; 
      } 
      //increment size 
      ++size; 

     } catch (int e) { 
      cout << "Error inserting " << item << " at position " << pos << endl; 
     } 
    } 
} 

template <class T> 
T LinkedList<T>::remove(int pos) { 

    //check bounds 
    if ((pos < 1) || (pos > getLength())) 
     cout << "Must remove a position between 1 and getLength()" << endl; 

    else { 
     try { 

      ListNode *temp; //to hold shifted node 

      //if node to be deleted is first node 
      if (pos == 1) { 
       temp = head; 
       head = head->next; 
      } 
      //for anything but the head 
      //write over the node before the pos'th index 
      else { 
       ListNode *prev = find(pos - 1); 
       temp = prev->next; 
       prev->next = temp->next; 
      } 

      //destroy temp, to free up memory 
      temp->next = NULL; 
      delete temp; 

      //decrement size 
      --size; 

     } catch (int e) { 
      cout << "Error removing item from position " << pos << endl; 
     } 
    } 


} 

#endif /* LINKEDLIST_HPP */ 
+0

@makc,和什麼? – soon

回答

1

ListNode是一個依賴型,因此它需要與typename加以限定。另外,您的find函數被聲明爲返回指向ListNode的指針,但該定義按值返回ListNode。這似乎是一個簡單的錯字:

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

明白了。謝謝! –

+1

您還有其他一些問題。你的析構函數應該是虛擬的,你需要使getLength爲const,以便它可以在find函數中使用,並且你不應該在頭文件中使用「namespace」。 –

1

資格的依賴型與typename,並添加<T>(我不知道這是必要的,還不如做它雖然):

typename LinkedList<T>::ListNode* LinkedList<T>::find(int pos) const { 

參見:Where and why do I have to put the "template" and "typename" keywords?

而且,你的定義不符。它在類中返回一個指針,但在它外面返回一個值。

+0

明白了。謝謝! –

0

應該是

LinkedList<T>::ListNode LinkedList<T>::find(int pos) const { 
+0

不是'ListNode'依賴於模板類型嗎? –

+0

@ K-ballo:是不是涵蓋'LinkedList :: ListNode' – Eric

+0

@Eric:不,它不是。 –

相關問題