我有一个嵌套模板在一个名为List::find()的方法的类中。
这个方法的输入是一个函数对象,即“Function condition”。
错误信息是:
为什么每个find()实现的运算符声明必须不同?
template<class T>
class List {
....
template<class Function>
Iterator find(Function condition) const;
....
};
template<class T, class Function>
typename List<T>::Iterator List<T>::find(Function condition) const {
List<int>::Iterator it = this->begin();
for (; it != this->end(); ++it) {
if (condition(*it)) {
break;
}
}
return it;
}
错误信息是:
..\list.h:108:62: error: invalid use of incomplete type 'class List<T>'
..\list.h:16:7: error: declaration of 'class List<T>'
如何引用List?为什么声明不正确?
编辑:
现在更改后为:
template<class T>
template<class Function>
我遇到了以下错误:
..\list.h:111:30: error: no match for 'operator++' in '++it'
..\list.h:112:18: error: no match for 'operator*' in '*it'
这是指这个操作符声明(其中之一):
template<class T>
typename List<T>::Iterator& List<T>::Iterator::operator++() {
List<T>::ConstIterator::operator++();
return *this;
}
为什么每个find()实现的运算符声明必须不同?
List<int>::Iterator
而不是List<T>::Iterator
。 - gx_