[英]no matching function for call to… error with template class
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. 我必须做一个通用的双链表,并且我在vc ++ 2010中做到了,并且一切正常,但是我必须用gcc进行编译,但是无法编译。 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. DLList位于.h文件中,并且每个方法都内联定义。 The iterator class is also in the DLList class. 迭代器类也位于DLList类中。
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) (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. 这允许传递从first()
和last()
返回的临时迭代器。 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
): 另外,您可以使用此函数签名并处理迭代器副本(例如,如果需要在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
. first()
和last()
是临时的,并且它们不能由Erase
签名中的引用绑定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.