i have to do a generic double linked list, and i made it in vc++ 2010, and everything worked well, but i have to compile it with gcc, but it can’t compile it. When i call a method which has an iterator as parameter, i got this error:
no matching function for call to 'DLList<int>::Erase(DLList<int>::iterator, DLList<int>::iterator)'|
[...]note: candidates are: void DLList<T>::Erase(DLList<T>::iterator&, DLList<T>::iterator&) [with T = int]|
The DLList is in a .h file, and every method defined inline. The iterator class is also in the DLList class.
template<typename T>
class DLList{
[...]
public:
[...]
void Erase(iterator &_first, iterator &_last){...}
iterator first(){...}
iterator last(){...}
[...]
class iterator{...}
[...]
};
And the code which causes the error:
iList.Erase(iList.first(), iList.last());
(iList: DLList< int> iList)
How can i fix it?
This allows your temporary iterators, returned from
first()andlast(), to be passed. You cannot get a non-const reference to a temporary.Alternatively, you could use this function signature and work on iterator copies (if you e.g. need to modify them within
Erase):