2012-10-06 127 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