该表达式无法分配什么?我正在尝试在C ++中实现双向链表。这是节点的struct
:
struct NodoLDL
{
T dato;
NodoLDL *anterior;
NodoLDL *siguiente;
NodoLDL(const T &elem, NodoLDL *ant = nullptr, NodoLDL *sig = nullptr):
dato(elem),
anterior(ant),
siguiente(sig)
{}
};
这是列表类:
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;
(. . .)
}
这是pop_front函数,我在listFront = temp;
上收到一条错误消息,该代码的含义是在每一行中作为注释:
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
}
}
为什么这种逻辑是错误的?我该如何解决?
答案 0 :(得分:1)
该函数更改调用它的对象的状态。使其成为const
成员函数没有任何意义。将其更改为非{const
成员函数。