mercredi 1 mai 2019

pop_front() with error "assignment of member in read-only object"

What is this expression unassignable? I am trying to implement a doubly linked list in c++. This is the struct of the Node:

struct NodoLDL
{
   T dato;
   NodoLDL *anterior;
   NodoLDL *siguiente;

   NodoLDL(const T &elem, NodoLDL *ant = nullptr, NodoLDL *sig = nullptr):
      dato(elem),
      anterior(ant),
      siguiente(sig)
   {}
};

And this is the list class:

template <typename T>
class LDL
{
private:
   #include "nodoldl.h"
   size_t listSize;
   NodoLDL *listFront; //head
   NodoLDL *listBack; //tail

public:
   LDL() : listSize(0), listFront(nullptr), listBack(nullptr)
   {}
   void pop_front() const;
(. . .)
}

This is the pop_front function, I get an error on listFront = temp;, what I meant to do with the code is in each following line as comments:

template<typename T>
void LDL<T>::pop_front() const
{
    if(empty()){
        throw invalid_argument("pop_front() on empty list");
    }else{
        NodoLDL *temp = listFront->siguiente; 
        ///temp points to the following node in the list (from listFront)

        temp->anterior = nullptr; 
        //Now that temp is at the "second" node in the list,
        //the pointer to the node before will be null, 
        //as the actual listFront will be deleted.

        delete listFront;
        //It deletes the node

        listFront = temp;
        //Now listFront is equal to temp, which is at the second node
    }
}

Why is that logic wrong? How can I fix it?

Aucun commentaire:

Enregistrer un commentaire