简体   繁体   中英

invalid conversion from 'const DList<int>* const' to 'DList<int>*' [-fpermissive] when returning an object

class DList{
    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* front_;
    Node* back_;
public:
    DList(){
        front_=nullptr;
        back_=nullptr;
    }
    void push_front(const T& data);
    ~DList();
class const_iterator{
    friend class DList;
    DList* myList_;
    Node* curr_;
    const_iterator(Node* curr, DList* theList){
        curr_ = curr;
        myList_ = theList;
    }
public:
    const_iterator(){
        myList_ = nullptr;
        curr_ = nullptr;
    }
    const_iterator operator++(){}
    const_iterator operator++(int){}
    const_iterator operator--(){}
    const_iterator operator--(int){}
    bool operator==(const_iterator rhs){}
    bool operator!=(const_iterator rhs){}
    const T& operator*()const{}
};
class iterator:public const_iterator{
    friend class DList;
    iterator(Node* curr, DList* theList):const_iterator(curr,theList){}
public:
    iterator():const_iterator(){}
    iterator operator++(){}
    iterator operator++(int){}
    iterator operator--(){}
    iterator operator--(int){}
    T& operator*(){}
    const T& operator*()const{}

};
const_iterator cbegin() const {
    return const_iterator(front_,this);
}
iterator begin(){
    return iterator(front_,this);

}
const_iterator cend() const{
    return const_iterator(nullptr,this);
}
iterator end(){
    return iterator(nullptr,this);

}
};

This is my assignment and the code for the linked list, my task is to implement the iterator classes for the linked list, I just implemented the begin and end functions, when I run the tester to check if those functions work, I got the error "invalid conversion from 'const DList* const' to 'DList*' [-fpermissive]" under the lines for these when returning a const_iterator object.

const_iterator cbegin() const {
    return const_iterator(front_,this);
}

const_iterator cend() const{
    return const_iterator(nullptr,this);
}

I did some research for this error message, but none of the answers help me understanding what my problem is. I cannot figure out why it is giving me this error, would appreciate help and clarifications.

const_iterator(Node* curr, DList* theList)

accepts a non- const DList * , but it is called with

    return const_iterator(front_, this);

from const method

const_iterator cbegin() const

The this in a const method is const , so here this is a const DList * and cannot be used as the argument for a parameter expecting a non- const DList * .

The trivial solution is to replace

    const_iterator(Node* curr, DList* theList)
    {
        curr_ = curr;
        myList_ = theList;
    }

with

    const_iterator(Node* curr, const DList* theList): 
        curr_(curr),
        myList_ (theList) // using member initializer list for reasons of taste
    {
    }

and follow const correctness all the way through, if possible. Eg member

DList* myList_;

must become

const DList* myList_;

or you've just moved the error message. There may be modifications required in the other member functions that are currently incompletely implemented in the question.

It looks to me like it should be possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM