链表Front()函数语法错误

Linked List Front() function syntax error

本文关键字:语法 错误 函数 Front 链表      更新时间:2023-10-16

我试图在我的链表类的Front()方法中返回一个节点。front方法只返回链表中的第一个元素(即它是一个节点)。然而,编译器不喜欢这样。它给了我以下错误:

Error: initial reference value of reference to non-const must be an lvalue.

有谁知道这个方法吗??

Node &List::Front()
{
return mHead->getData();
}
Potion Node::getData()
{
return mData;
}

list.h

#ifndef LIST_H
#define LIST_H
#include "node.h"
#include "potion.h"
class List
{
public:
//Default constructor
List();
//Copy constructor
List(const List & copy);
//Overloaded assignment operator
List &operator=(const List & rhs);
//Destructor
~List();
//Methods
void PushFront(Node * newNode);
void PushBack(Node * newNode);
void PopFront();
void PopBack();
/*const Potion &Front() const;
const Potion &Back() const;*/
Node &Front();
Node &Back();
void Purge();
bool empty();
int getNumberOfNodes();
Node * CreateNode(Potion potion);
void Save(std::ofstream & file);
void Load(std::ifstream & file);
protected:
//Data members
Node * mHead;
Node * mTail;
static int mNumberOfNodes;
};
#endif

我猜您的getData()函数返回数据的副本而不是引用。临时对象不是lvalue