Getting read-only object error when incrementing a variable

  c++, compiler-errors, g++

I am getting the following error on compilation:

list.h:246:20: error: assignment of member ‘DList<int>::count’ in read-only object
  246 |  it.mylist_->count = 2;

Here is the program that I have written. I am basically trying to write an insert function using the list iterator.

template <typename T>

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_;
    int count;

public:
    class const_iterator
    {
        friend class DList;

    protected:
        const DList *mylist_;
        Node *curr_;
        const_iterator(Node *n, const DList *list)
        {
            curr_ = n;
            mylist_ = list;
        }

    public:
        const_iterator()
        {
            curr_ = nullptr;
            mylist_ = nullptr;
        }
        const_iterator &operator++()
        {
            curr_ = curr_->next_;
            return *this;
        }
        const_iterator operator++(int)
        {
            const_iterator old = *this; // captures the state of orginal pointer
            curr_ = curr_->next_;       // update current object
            return old;                 // return the orginal
        }
        const_iterator &operator--()
        {
            if (mylist_ != nullptr && curr_ == nullptr)
            {
                curr_ = mylist_->back_;
            }
            else
            {
                curr_ = curr_->prev_;
            }

            return *this;
        }
        const_iterator operator--(int)
        {
            const_iterator old = *this;
            if (mylist_ != nullptr && curr_ == nullptr)
            {
                curr_ = mylist_->back_;
            }
            else
            {
                curr_ = curr_->prev_;
            }
            return old;
        }
        bool operator==(const_iterator rhs)
        {
            bool rc = false;
            if (this->mylist_ == rhs.mylist_ && this->curr_ == rhs.curr_)
                rc = true;
            return rc;
        }
        bool operator!=(const_iterator rhs)
        {
            return !(*this == rhs);
        }
        const T &operator*() const
        {
            return curr_->data_;
        }
    };
    class iterator : public const_iterator
    {
        friend class DList;

    protected:
        iterator(Node *curr, DList *list) : const_iterator(curr, list) {}

    public:
        iterator() : const_iterator() {}
        iterator &operator++()
        {
            this->curr_ = this->curr_->next_;
            return *this;
        }
        iterator operator++(int)
        {
            iterator old = *this;             // captures the state of orginal pointer
            this->curr_ = this->curr_->next_; // update current object
            return old;                       // return the orginal
        }
        iterator &operator--()
        {
            if (this->mylist_ != nullptr && this->curr_ == nullptr)
            {
                this->curr_ = this->mylist_->back_;
            }
            else
            {
                this->curr_ = this->curr_->prev_;
            }

            return *this;
        }
        iterator operator--(int)
        {
            iterator old = *this;
            if (this->mylist_ != nullptr && this->curr_ == nullptr)
            {
                this->curr_ = this->mylist_->back_;
            }
            else
            {
                this->curr_ = this->curr_->prev_;
            }
            return old;
        }
        T &operator*()
        {
            return this->curr_->data_;
        }
        const T &operator*() const
        {
            return this->curr_->data_;
        }
    };
    DList();
    ~DList();
    DList(const DList &rhs);
    DList &operator=(const DList &rhs);
    DList(DList &&rhs);
    DList &operator=(DList &&rhs);
    iterator insert(iterator it, const T &data);
    iterator search(const T &data);
    iterator erase(iterator it);
    void sort(iterator first, iterator last);
    bool empty() const;
    int size() const;

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

template <typename T>
DList<T>::DList()
{
    front_ = nullptr;
    back_ = nullptr;
    count = 0;
}
template <typename T>
DList<T>::~DList()
{
    Node *curr = front_;
    while (curr != nullptr)
    {
        Node *next = curr->next_;
        delete curr;
        curr = next;
    }
}
template <typename T>
DList<T>::DList(const DList &rhs)
{
}
template <typename T>
DList<T> &DList<T>::operator=(const DList &rhs)
{
}
template <typename T>
DList<T>::DList(DList &&rhs)
{
}

template <typename T>
DList<T> &DList<T>::operator=(DList &&rhs)
{
}

template <typename T>
typename DList<T>::iterator DList<T>::insert(iterator it, const T &data)
{
    Node *nn = new Node(data, front_);
    Node *curr = it.curr_;
    if (it.mylist_->empty())
    {
        front_ = nn;
        back_ = nn;
        nn->next_ = nullptr;
        nn->prev_ = nullptr;
    }
    else
    {
        nn->prev_ = curr->prev_;
        nn->next_ = curr;
        curr->prev_->next_ = nn;
    }
    int currentCount = it.mylist_->count;
    it.mylist_->count = 2;
    it--;
    return it;
}

template <typename T>
typename DList<T>::iterator DList<T>::search(const T &data)
{
}

template <typename T>
typename DList<T>::iterator DList<T>::erase(iterator it)
{
}

template <typename T>
void DList<T>::sort(iterator first, iterator last)
{
}
template <typename T>
bool DList<T>::empty() const
{
    bool check = false;
    if (front_ == nullptr || back_ == nullptr)
    {
        check = true;
    }
    return check;
}
template <typename T>
int DList<T>::size() const
{
    return count;
}

Once the insertion is done the count needs to be updated. I am not sure how to achieve that as it’s giving a read-only error
I am implementing a doubly linked list here with iterators. And providing some functions to manipulate the linked list using iterators.
Source: Windows Questions C++

LEAVE A COMMENT