C++ 函数对象和模板:错误:声明 'class List<T>'

4
我有一个嵌套模板在一个名为List::find()的方法的类中。 这个方法的输入是一个函数对象,即“Function condition”。
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()实现的运算符声明必须不同?
1个回答

5
不是
template<class T, class Function>
typename List<T>::Iterator List<T>::find(Function condition) const {
   ...
}

但是,更确切地说
template<class T>
template<class Function>
typename List<T>::Iterator List<T>::find(Function condition) const {
   ...
}

您必须“分开”这两个template<...>(第一个是类的模板,第二个是成员函数的模板)。


@Eitan,也许这并不能解决你的新错误,但是在函数内部你使用了List<int>::Iterator而不是List<T>::Iterator - gx_
4
现在您遇到了一个新错误,这就成为了一个新问题。您可能需要为此启动一个新的问题。 - Adrian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接