2013-05-07 13 views
0

我想在我的鏈接列表類的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 
+0

不應該是'return * mHead;'?你想返回一個'Node'而不是'Potion'。 – 2013-05-07 20:53:02

+0

@JesseGood非常感謝! – MrPickle5 2013-05-07 20:59:53

回答

3

我猜你getData()函數返回的數據的副本,而不是引用。臨時對象不是lvalue

+0

我只是將它改爲通過引用返回,現在它說'錯誤:類型爲「節點&」(不是const限定的)的引用無法使用Potion的值類型進行初始化 – MrPickle5 2013-05-07 20:55:47

+2

@ MrPickle5,您的簽名表示您'重新返回'Node&',但你正在嘗試返回一個'Potion&'。你需要選擇一個或另一個並保持一致。 – 2013-05-07 20:58:37