Reputation: 522
I am trying to return a Node in my Front() method of my linked list class. The front method just returns the first element in the linked list (i.e. which is a node). The compiler does not like this, however. It gives me the following error.
Error: initial reference value of reference to non-const must be an lvalue.
Does anyone know a way around this?? mHead is what is underlined by 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
Upvotes: 0
Views: 1031
Reputation: 308382
I'd guess that your getData()
function is returning a copy of the data rather than a reference. A temporary object isn't an lvalue
.
Upvotes: 3