C ++ - functors and templates: error: declaration of class List <T> '

I have a nested template on the side of the template class for a method called List :: find (). This method receives a functor as an input, namely: "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; } 

Error:

 ..\list.h:108:62: error: invalid use of incomplete type 'class List<T>' ..\list.h:16:7: error: declaration of 'class List<T>' 

How should I reference List? Why is the statement wrong?

Edit:

Now after switching to:

 template<class T> template<class Function> 

I get the following errors:

 ..\list.h:111:30: error: no match for 'operator++' in '++it' ..\list.h:112:18: error: no match for 'operator*' in '*it' 

which relate to this operator declaration (one of them):

 template<class T> typename List<T>::Iterator& List<T>::Iterator::operator++() { List<T>::ConstIterator::operator++(); return *this; } 

Why should the declaration of this statement be different for each find () implementation?

+4
source share
1 answer

Not

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

but rather

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

You must โ€œseparateโ€ the two template<...> (the first for the class, the second for the member function).

+5
source

Source: https://habr.com/ru/post/1486199/


All Articles