c++ - 链表Front()函数语法错误

标签 c++ linked-list

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

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

有人知道解决这个问题的方法吗? mHead 是 intellisense 下划线的部分。

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() 函数正在返回数据的拷贝而不是引用。临时对象不是左值

关于c++ - 链表Front()函数语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16428282/

相关文章:

c++ - 让派生类不继承基类函数

c++ - 无法让我的指针发送对数组的引用

c++ - Fortran C++ 链接库

c# - 如何在 C# 中将 LinkedList<T> 添加到 LinkedList<T>?

c++ - 使用运算符读取 .CSV 文件 >>

c++ - C++ 模板中的自定义函数

c - C中链表的结构

java - 迭代器方法?

c++ - C++链表中私有(private)指针错误

java - 删除单链表中间的节点