Domanda

This is a linked list, and I'm trying to overload ==, it's giving me the error: error: passing 'const Linked_List as 'this' argument of 'ItemType Linked_List::Element(int) [with ItemType = int]' discards qualifiers.

I have to check if both linked lists have the same number of elements, and if each element is equal pairwise.

The error points to the implementation part of == Here's the excerpt: P.S: element(i) returns the value in the i position of the linked list

template <typename ItemType>
bool Linked_List <ItemType>::operator == (const Linked_List &eq)const {
    if (eq.Items != Items){
        return false;
    }
    if (eq.Items == 0){
        return true;
    }
    for (int i = 0; i < eq.Items; i++){
        if (eq.Element(i) != Element(i)){     //error points here
            return false;
        }
    }
    return true;
}

Here's the rest of my code that might be relevant. I didn't post all my code since everything is working fine including element(), only the overloading won't work.

//.h
template <typename ItemType>
class Node
{
    public:
        ItemType Data;
        Node <ItemType> *next;
};

template <typename ItemType>
class Linked_List
{
    public:   
        Node <ItemType> *start;
        int Items;
        Linked_List();
        ItemType Element(int num);
        bool operator == (const Linked_List &eq)const;
}

.

.//cpp
#include "Linked_List.h"
template <typename ItemType>
Linked_List <ItemType>::Linked_List(){
    start = NULL;
    Items = 0;
}
template <typename ItemType>
ItemType Linked_List <ItemType>::Element(int num){
    ItemType result = 0;
    if (start == NULL){
        return result;
    }
    Node <ItemType> *nnode;
    nnode = start;
    int current_position = 0;
    while (current_position < num){
        current_position++;
        nnode = nnode -> next;
    }

    result = nnode -> Data;
    nnode = NULL;
    delete nnode;
    return result;
}
template <typename ItemType>
bool Linked_List <ItemType>::operator == (const Linked_List &eq)const {
    if (eq.Items != Items){
        return false;
    }
    if (eq.Items == 0){
        return true;
    }
    for (int i = 0; i < eq.Items; i++){
        if (eq.Element(i) != Element(i)){      //error
            return false;
        }
    }
    return true;
}

int main(){
    Linked_List <int> test8;
    Linked_List <int> test7;
    cout << (test8 == test7) << endl;
}
È stato utile?

Soluzione

Methods declared as const can only call other const methods. They cannot non-const methods. In your case method operator == is declared as const. From inside of operator == you are attempting to call method Element, which is non-const. This is your error.

Moreover, both Element calls in

if (eq.Element(i) != Element(i))

are invalid. The first call is invalid since eq is a const reference, meaning that you cannot call any non-const methods through it. The second call is invalid for the reasons described above.

Either declare your Element method as const or provide a second, const version of Element in addition to the non-const one. I see that your element returns its result by value, which means that you can simply declare it as const.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top