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?
void Erase(iterator const &_first, iterator const &_last){...}
This allows your temporary iterators, returned from first()
and last()
, 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 eg need to modify them within Erase
):
void Erase(iterator _first, iterator _last){...}
The problem is that a non-const reference cannot be bound to a temporary. The result of first()
and last()
are temporaries, and those cannot be bound by the references in the signature of Erase
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.