2012-10-06 126 views
1

我在使用基本代碼this data structures book實現鏈接列表的搜索功能時遇到了一些麻煩。這是我得到的錯誤:錯誤:'operator =='不匹配

llist.h: In member function 'void LList<E>::search(const E&, bool&) [with E = Int]': 
Llistmain.cpp:31:1: instantiated from here 
llist.h:119:3: error: no match for 'operator==' in '((LList<Int>*)this)->LList<Int>::curr->Link<Int>::element == value' 

這裏是我的搜索成員函數的實現:

void search(const E& value, bool& found) { 
    if (curr->element == value) 
     found = true; 
    else if (curr != NULL) { 
     curr = curr->next; 
     search(value, found); 
    } 
    else found = false; 
} 

爲什麼我會得到一個錯誤有關== operatorcurr->elementvalue都是Int類型。我應該以不同的方式檢查平等嗎?

回答

0

根據該錯誤,element不是int而是Link<Int>。您需要從Link中獲取Int,並將其變爲operator==(請注意,Int不是int)。

1

您的類型Int是否有比較運算符?如果有,它是否將其兩個參數都作爲const?特別是,如果你比較運營商成員,很容易忘記,使之成爲const成員:

bool Int::operator== (Int const& other) const { 
    ... 
} 
+0

這解決了這個問題。我爲Int提出了一個重載的比較運算符,並解決了問題。謝謝。 – beastmaster