Invalid initialization of non-const reference of type

Hey guys, so I'm trying to create a queue using a linked list I've written. But when I try to compile I get an error saying:

"queue.cpp: In member function ‘banfield_lab6::node::value_type& banfield_lab6::queue::front()’:
queue.cpp:18:25: error: invalid initialization of non-const reference of type ‘banfield_lab6::node::value_type& {aka int&}’ from an rvalue of type ‘banfield_lab6::node::value_type {aka int}’
return data.getCurrent();"

I was wondering whether anyone would be kind enough to explain what I'm doing wrong. Here's the implementation of front() from the queue.cpp:

1
2
3
4
5
node::value_type& queue::front(){

	data.moveToHead();
	return data.getCurrent();
}


And here's the implementation of getCurrent() from linkedlist.cpp(value_type is just an alias for int at the moment):

1
2
3
node::value_type linkedlist::getCurrent() const{
	return current_ptr->getData();
	}
'getCurrent' returns a value.
'front' returns a reference.

If you return getCurrent() from front(), you are returning a reference to a copy of a variable (sine getCurrent returns by value instead of by reference).

A possible solution is to have getCurrent (and possibly getData) return a reference rather than a value, but it's difficult to say whether or not that's a good idea without seeing the overall design.
Ah ok, I was able to fix it by making front() return a value instead of a reference.
Topic archived. No new replies allowed.