Reputation: 171
I THINK this is called a temporary object. Here is my scenario:
I have some functions:
void List<T>::Remove(ListIterator &)
ListIterator List<T>::Begin() //returns an iterator pointing to the first node in the list.
In G++ when I try to do something like this:
mylist.Remove(mylist.Begin())
I get the following error:
no matching function for call to Remove(ListIterator).
I don't have a function that takes a ListIterator by value and nor do I want one. However I'm not sure if what I'm trying to do even makes sense. The code works on windows VS2012 but not in g++.
Any advice on how to either fix my implementation or an explanation of this behaviour is greatly appreciated!
Upvotes: 0
Views: 487
Reputation:
Kris, if you see the error compiler gave (in your first message) "no matching function for call to Remove(ListIterator)" here the compiler states the ListIterator is passed to Remove as parameter whereas it expects ListIterator&, so to resolve change the func declaration Begin to return ListIterator& , or as Kris suggested, change Remove func declaration to take const ListIterator & as parameter. I hope both options should work.
Upvotes: 0
Reputation: 153909
The results of mylist.Begin()
are a temporary (unless
mylist.Begin()
returns a reference). You can't use
a temporary to initialize a non-const reference. Either assign
the results to a variable, and pass it to Remove
, make the
reference const, or use pass by value.
As to why VS2012 compiles it: VS2012 isn't conform.
Upvotes: 1