operator == overload for double linked list

c++ linked list overload
c++ linked list overload
overload assignment operator c++ linked list
operator overloading
linked list operator[]
doubly linked list assignment operator
overload subscript operator linked list c++
linked list operator

I have realized Double Linked List. And now i need to overload == operator.

dll.cpp:67:17: error: expected expression if ([ind] != sp[ind]) {

The problem i don't understand how to overload == operator if only one parameter is given. I mean if i write bool operator ==(DLL sp1, DLL sp2){} compiler says error: overloaded 'operator==' must be a binary operator (has 3 parameters)

using namespace std;

template<typename T>
class 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;
        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)
        delete tail;
    int length() {return size;}
    class Node{
        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;

You're doing the right thing for the most part.

The problem that is causing this error:

dll.cpp:67:17: error: expected expression if ([ind] != sp[ind]) {

is that you actually want to do this instead:

*this[ind] != sp[ind]

Also, there appears to be an extra } right here:

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

Operator Overloading '<<' and '>>' operator in a linked list class , Prerequisite: Operator Overloading in C++, Linked List in C++ We can overload the '>>' and '<<' operators to take input in a linked list and print if (head == NULL) Kth node from beginning with Kth node from end in a Doubly Linked List . I&#39;m trying to overload the assignment operator. The objective is to copy two linked lists. I copy the first element fine, but for some reason

The way you have it defined as a member function (and it only takes list of int for some reason (you should probably remove the <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.

When the compiler see's this.

list1 == list2

This is simply syntactic sugar for:


Thus is why you only need one parameter when you declare it as a member function. The alternative is to declare it as a friend function.

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

In this case it is a free standing function. When the compiler sees:

list1 == list2

This is syntactic sugar for:

operator==(list1, list2)

The problem was that you were defining a member function with two parameters. The left hand side is the class object and then you were expecting two objects on the right hand side (but the == operator only has one place on the right). That is why it is complaining about three parameters.

So the real question is should it be a member or a friend.

It does not matter here.

There are situations where it "can".

Example: If your class contains a single argument constructor (lets say you could create a list from an integer) and you use a member operator==()

 DLL<int>    list;

 if (list == 5) {

This will now compile. Because your member operator uses a parameter and the compiler can convert an integer into the DLL parameter using a single argument constructor.

The counter argument to this is that normally you don't want automatic conversions of your type so you should mark single argument constructors as explicit to prevent this.


If you class can automatically be created via a one argument constructor (most cases this is not true but it can be).

Then you should prefer a friend function version.

otherwise it does not matter and I would probably fall towards the member function.

Problem: Assignment Operator Overloading Of A Doubly Linked List , problem: Assignment operator overloading of a doubly linked list: 105, if (pAfter == NULL && pBefore ==NULL)� Construct a Doubly linked linked list from 2D Matrix Find the string formed by joining k consecutive nodes of linked list Operator Overloading '<<' and '>>' operator in a linked list class

A comparison operator is a binary operator that treats both the operands equally, it is advisable to make it a friend function and not a member.

So the declaration for the function would change to

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

You can choose to define it inside or outside the class.

Read here on when you need to make an operator member vs non-member.

[13] Operator overloading, C++ FAQ Lite, [13.6] Can I overload operator== so it lets me compare two char[] using a string The LinkedList class could have its internals replaced with a doubly linked list,� Operators Overloading in C++. You can redefine or overload most of the built-in operators available in C++. Thus, a programmer can use operators with user-defined types as well. Overloaded operators are functions with special names: the keyword "operator" followed by the symbol for the operator being defined.

Overloading << and [] operators for Link - C++ Forum, I'm trying to overload the [] operator for my linked list class so that you can NULL;current = current->link) { if (i == pos) { return current->data; }� Canonical implementations. Other than the restrictions above, the language puts no other constraints on what the overloaded operators do, or on the return type (it does not participate in overload resolution), but in general, overloaded operators are expected to behave as similar as possible to the built-in operators: operator + is expected to add, rather than multiply its arguments, operator

[13] Operator overloading Updated! , C++ FAQ Lite, [13.6] Can I overload operator== so it lets me compare two char[] using a string strong promises regarding that infrastructure (e.g., if it was a doubly linked list,� Operator overloadability. A user-defined type can overload the == and != operators. If a type overloads one of the two operators, it must also overload the other one. C# language specification. For more information, see the Relational and type-testing operators section of the C# language specification. See also. C# reference; C# operators

Doubly Linked List Copy Constructor And Overloading = Operator, [13.6] Can I overload operator== so it lets me compare two char[] using a string if it were a doubly linked list, it might guarantee that every forward pointer was� Question: C++ Overload The == Operator In UnorderedLinkedList That Returns True When This List Has Exactly The Same Elements As The Argument List Regardless Of Order. Write A Function Called GetKthElement That Returns The Element At Position K Given An Integer K. Include Cpp File And Run Test.

  • you should also adress the T vs int issue. If OP adds this to the class declaration then DLL<T> will be befriended with the operator for DLL<int> which isnt really the right thing to do