2012-10-02 15 views
1

我正嘗試在雙向鏈表中使用自定義數據類型。我能夠創建列表,但是當我嘗試調用插入函數時,它出錯了。什麼導致這個錯誤?雙向鏈表中的自定義數據類型

main.cpp中:

#include "command.h" //my custom class 
#include "doublyLinkedList.h" 
int main(){ 

    //create a queue of jobs: 
    doublyLinkedList<command>* queue = new doublyLinkedList<command>; 

    //creating a new command: 
    command *c = new command(); 
    c->createCommand(); 
    c->print(); 
    const command *d = new command(c->name, c->description, c->shellString); 

    queue->insert(*d); //problem line 
}; 

doublyLinkedList.h的插入功能:

template <class Type> 
void doublyLinkedList<Type>::insert(const Type& insertItem) 
{ 
nodeType<Type> *current;  //pointer to traverse the list 
nodeType<Type> *trailCurrent; //pointer just before current 
nodeType<Type> *newNode;  //pointer to create a node 
bool found; 

newNode = new nodeType<Type>; //create the node 
newNode->info = insertItem; //store the new item in the node 
newNode->next = NULL; 
newNode->back = NULL; 

if(first == NULL) //if the list is empty, newNode is 
        //the only node 
{ 
    first = newNode; 
    last = newNode; 
    count++; 
} 
else 
{ 
    found = false; 
    current = first; 

    while (current != NULL && !found) //search the list 
     if (current->info >= insertItem) 
      found = true; 
     else 
     { 
      trailCurrent = current; 
      current = current->next; 
     } 

    if (current == first) //insert newNode before first 
    { 
     first->back = newNode; 
     newNode->next = first; 
     first = newNode; 
     count++; 
    } 
    else 
    { 
      //insert newNode between trailCurrent and current 
     if (current != NULL) 
     { 
      trailCurrent->next = newNode; 
      newNode->back = trailCurrent; 
      newNode->next = current; 
      current->back = newNode; 
     } 
     else 
     { 
      trailCurrent->next = newNode; 
      newNode->back = trailCurrent; 
      last = newNode; 
     } 

     count++; 
    }//end else 
}//end else 
}//end insert 

的代碼適用於整數和字符串,而不是 「命令」 類型。

錯誤:

In file included from main.cpp:2:0: 
doublyLinkedList.h: In member function âvoid doublyLinkedList<Type>::insert(const Type&) [with Type = command]â: 
main.cpp:34:18: instantiated from here 
doublyLinkedList.h:171:13: error: no match for âoperator>=â in âcurrent->nodeType<command>::info >= insertItemâ 
doublyLinkedList.h:171:13: note: candidates are: 
/usr/include/c++/4.6/bits/stl_pair.h:232:5: note: template<class _T1, class _T2> bool std::operator>=(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) 
/usr/include/c++/4.6/bits/stl_iterator.h:315:5: note: template<class _Iterator> bool std::operator>=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&) 
/usr/include/c++/4.6/bits/stl_iterator.h:365:5: note: template<class _IteratorL, class _IteratorR> bool std::operator>=(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&) 
/usr/include/c++/4.6/bits/basic_string.h:2621:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator>=(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&) 
/usr/include/c++/4.6/bits/basic_string.h:2633:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator>=(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) 
/usr/include/c++/4.6/bits/basic_string.h:2645:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator>=(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&) 
+0

每次我發佈完整的代碼,我捱罵...... – Jeff

+0

那麼考慮:這很重要,這是一個雙向鏈表,而不是一個單鏈接一個?嘗試刪除第二個鏈接,然後再詢問並查看。如果你把東西直接推到頭上或者如果你把它分類,這有什麼關係?嘗試刪除排序代碼並查看。堅持:如果您嘗試對其進行排序,它*確實很重要?發現吸菸槍。你的問題轉化爲*「爲什麼我可以比較整數和字符串,但不是'command'參考?」*你甚至可以自己回答... http://sscce.org/ – HostileFork

回答

3

看來你試圖保持排序的名單,但是你還沒有定義的比較運營商command。如果您想按原樣使用,則需要爲您的類型實施operator>=

它的簽名很可能是這個樣子:

bool operator>= (const command& a, const command& b); 
+0

重載操作符?我會試試這個,讓你知道它是如何工作的! – Jeff

+0

http://en.wikipedia.org/wiki/Operator_overloading,或者當在Google中查找「操作符重載」時,第一頁上的任何其他結果都應該有所幫助。 –

+0

哦,我以前做過。我只是要求澄清。 – Jeff