运算符==重载双链表

时间:2019-11-28 06:02:29

标签: c++ class operator-overloading

我已经实现了双链表。现在我需要重载==运算符。

dll.cpp:67:17:错误:预期表达             如果([ind]!= sp [ind]){

如果只给出一个参数,我不明白如何重载==运算符。我的意思是,如果我写布尔运算符==(DLL sp1,DLL sp2){},编译器说error: overloaded 'operator==' must be a binary operator (has 3 parameters)

#include<iostream>
#include<string>
using namespace std;

template<typename T>
class DLL {
public:
    DLL(){
        size = 0;
        head = nullptr;
        tail = nullptr;
    }
    T operator [](int ind) {
        int counter = 0;
        T res = T();
        Node *cur = this->head;
        while(cur != nullptr) {
            if(counter == ind) {
                res = cur->data;
            }
            cur = cur->next;
            counter++;
        }
        return res;
    }
    bool operator ==(DLL<int> sp){
        bool isequal = true;
        for(int ind = 0; ind < sp.length(); ind++){
            if ([ind] != sp[ind]) {
                isequal = false;
            }
        }
        return isequal;

    }
    void clear() {
        while(size != 1)
            pop_front();
        delete tail;
        size--;
    }
    int length() {return size;}
    }
private:
    class Node{
    public:
        T data;
        Node *next;
        Node *prev;
        Node(T data = T(), Node *prev= nullptr, Node *next = nullptr) {
            this->data = data;
            this->next = next;
            this->prev = prev;
        }
    };
    int size;
    Node *head;
    Node *tail;
};

3 个答案:

答案 0 :(得分:3)

将其定义为成员函数的方式(由于某种原因,它仅获取int的列表(您可能应该删除<int>)。

bool operator ==(DLL<int> sp);   // There is a small issue in that you
                                 // passing by value and thus causing a copy.

                                 // Another issue with this is that it should
                                 // probably marked "const" to indicate state
                                 // is not changed by the call.

当编译器看到此内容时。

list1 == list2

这仅仅是语法糖:

list1.operator==(list2);

这就是为什么在将其声明为成员函数时只需要一个参数的原因。另一种方法是将其声明为朋友函数。

friend bool operator ==(DLL<T> const& lhs, DLL<T> const& rhs);

在这种情况下,它是一个独立功能。当编译器看到:

list1 == list2

这是用于以下方面的语法糖:

operator==(list1, list2)

问题是您要使用两个参数定义一个成员函数。左手边是类对象,然后您希望在右手边有两个对象(但是==运算符在右边只占一个位)。这就是为什么它抱怨三个参数。

所以真正的问题是应该是成员还是朋友。

这没关系。

在某些情况下会“可以”。

示例:如果您的类包含单个参数构造函数(假设您可以从整数创建列表),并且使用成员operator==()

 DLL<int>    list;

 if (list == 5) {
 }

现在将进行编译。因为您的成员运算符使用参数,并且编译器可以使用单个参数构造函数将整数转换为DLL参数。

与此相反的是,通常您不希望自动转换类型,因此应将单参数构造函数标记为explicit,以防止这种情况。

所以:

如果可以通过一个参数构造函数自动创建类(大多数情况下不是,但是可以)。

然后,您应该选择一个朋友功能版本。

否则没关系,我可能会偏向成员函数。

答案 1 :(得分:0)

比较运算符是对两个操作数均等对待的二进制运算符,建议将其设为好友函数而不是成员函数。

因此该函数的声明将更改为

id   |   parent_id   |    criteria_1   |   criteria_2
------------------------------------------------------
1    |    1          |    523          |   563
2    |    1          |    null         |   null
3    |    2          |    null         |   null
4    |    2          |    null         |   null
5    |    3          |    777          |   null

您可以选择在类的内部或外部定义它。

请阅读here,了解何时需要成为操作员或非操作员。

答案 2 :(得分:0)

在大多数情况下,您所做的都是正确的事情。

导致此错误的问题:

  

dll.cpp:67:17:错误:如果([ind]!= sp [ind]){

是您实际上要执行此操作:

*this[ind] != sp[ind]

此外,这里似乎还有一个}

int length() {return size;}
} // <- not exactly sure what that's about, but I don't think you need it.
相关问题