与运营商不匹配!=

时间:2017-09-26 02:19:46

标签: c++ list iterator

我正在尝试创建链表类并定义迭代器,除了最后一个,我都拥有它们。我不知道如何修复,我编译代码时出现此错误:

  

在'recList.SortedList :: begin with T = Record!= recList.SortedList :: end with T = Record'中'operator!='不匹配   a1q1main.cpp:114:37:注意:候选人是:   sortedlist.h:112:11:注意:SortedList :: iterator SortedList :: iterator :: operator!=(bool)[with T = Record,SortedList :: iterator = SortedList :: iterator]   sortedlist.h:112:11:注意:参数1从'SortedList :: iterator'到'bool'没有已知的转换

无论我做什么,它都会继续显示此错误 我已经声明了operator ==并且一切都很好,但是!=抱怨 这是代码:

 class SortedList {
 struct Node {
    T data_;
    Node* next_;
    Node* prev_;
    Node(const T& data = T{}, Node* next = nullptr, Node* prev = nullptr) {
        data_ = data;
        next_ = next;
        prev_ = prev;
    }
};
Node* head_;
Node* tail_;
    public:
class const_iterator {
protected:
    Node* curr_;
public:
    const_iterator(Node* p) {
        curr_ = p;
    }
 .........
    const_iterator operator--(int) {
        const_iterator tmp = *this;
        curr_ = curr_->prev_;
        return tmp;
    }
    const T& operator*() const {
        return curr_->data_;
    }


     const_iterator operator==(bool){
            return false;
     }

    const_iterator operator!=(bool){
            return true;
    }

return;`

我需要满足以下条件:  operator!= 如果两个迭代器指向不同的节点,则返回true,否则返回false O(1)

我没有完成运算符的逻辑,我只需要正确声明它所以我没有得到错误

1 个答案:

答案 0 :(得分:1)

运算符重载的签名不正确。你让他们接受一个bool并返回一个迭代器。它应该是另一种方式。

考虑您正在执行的操作

SharedPreferences myPreferences =getSharedPreferences("YourprefereneName",MODE_PRIVATE)

你甚至在你的函数中返回一个bool,尽管签名需要一个迭代器作为返回值。

而是在所需的签名中实现运算符重载

if(it1 == it2){
    // do stuff
}

请注意,您可以在bool operator==(sorted_list_iterator it){ return (curr_->data_ == it.curr_->data_); } bool operator!=(sorted_list_iterator it){ return !(*this == it); } 中使用operator==重载,以避免在两个函数中重复相等逻辑。您可能还需要在这些函数中允许空operator!=

相关问题