Griffosx
Griffosx

Reputation: 1625

c++ list, no viable conversion from 'value_type'

I'm using list in c++

std::list<Foo> foo;
foo.push_back(bar);
Foo *ret = foo.back();

When compile I obtain this error (at last line):

No viable conversion from 'value_type' (aka Foo) to 'Foo *'

How can I solve this?

Thanks

Upvotes: 1

Views: 6561

Answers (1)

Peter Bloomfield
Peter Bloomfield

Reputation: 5766

foo.back() will return a reference (i.e. Foo &), but you're trying to assign it to a pointer.

To get it to work, you can assign to a reference instead:

Foo &ret = foo.back();

Or get the address of the item returned, and assign that to a pointer:

Foo *ret = &foo.back();

Upvotes: 3

Related Questions