错误:'operator=='不匹配

error: no match for 'operator=='

本文关键字:不匹配 operator 错误      更新时间:2023-10-16

我在使用这本数据结构书中的基本代码实现链表的搜索功能时遇到了一些问题。这是我得到的错误:

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;
}

为什么我收到有关== operator的错误?curr->elementvalue 都是 Int 类型。我应该以不同的方式检查相等性吗?

您的类型Int是否有比较运算符?如果有,它是否将其两个论点都视为const?特别是,如果您比较运算符是成员,则很容易忘记将其设为const成员:

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

根据错误,element不是int而是Link<Int>。您需要将IntLink中取出,并将其转换为具有operator==的东西(请注意,Int不是int)。