dimanche 26 juillet 2015

Unable to get std::find_if to compile with custom iterator

This is the line causing the issue:

auto it = find_if(menus->begin(), menus->end(), [](MenuComponent* m){return true;});

My iterator is defined as:

class ComponentIterator : std::iterator< std::forward_iterator_tag, MenuComponent* > 

It has ++, ==, !=, *, -> all defined. This iterator also works with the following line:

auto it = copy_if(menus->begin(), menus->end(), othermenu.begin(), [](MenuComponent* m){return true;});

g++ 4.8.1 output is as follows:

In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from main.cpp:2:
/usr/include/c++/4.8/bits/stl_algo.h: In instantiation of ‘_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = ComponentIterator; _Predicate = main()::__lambda0]’:
main.cpp:211:97:   required from here
/usr/include/c++/4.8/bits/stl_algo.h:4465:40: error: no matching function for call to ‘__iterator_category(ComponentIterator&)’
        std::__iterator_category(__first));
                                        ^
/usr/include/c++/4.8/bits/stl_algo.h:4465:40: note: candidate is:
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:65:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/string:40,
                 from main.cpp:1:
/usr/include/c++/4.8/bits/stl_iterator_base_types.h:201:5: note: template<class _Iter> typename std::iterator_traits<_Iterator>::iterator_category std::__iterator_category(const _Iter&)
     __iterator_category(const _Iter&)
     ^
/usr/include/c++/4.8/bits/stl_iterator_base_types.h:201:5: note:   template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/stl_iterator_base_types.h: In substitution of ‘template<class _Iter> typename std::iterator_traits<_Iterator>::iterator_category std::__iterator_category(const _Iter&) [with _Iter = ComponentIterator]’:
/usr/include/c++/4.8/bits/stl_algo.h:4465:40:   required from ‘_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = ComponentIterator; _Predicate = main()::__lambda0]’
main.cpp:211:97:   required from here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h:201:5: error: no type named ‘iterator_category’ in ‘struct std::iterator_traits<ComponentIterator>’
make: *** [main.o] Error 1

Any ideas?

Aucun commentaire:

Enregistrer un commentaire