我想在我的鏈接列表類的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
不應該是'return * mHead;'?你想返回一個'Node'而不是'Potion'。 – 2013-05-07 20:53:02
@JesseGood非常感謝! – MrPickle5 2013-05-07 20:59:53