0

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
1
  • Shouldn't that be return *mHead;? You want to return a Node not a Potion. Commented May 7, 2013 at 20:53

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I just changed it to return by reference and now it is saying error: a reference of type "Node &" (not const-qualified) cannot be initialized with a value type of Potion
@MrPickle5, your signature says you're returning Node& but you're trying to return a Potion&. You need to pick one or the other and be consistent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.