无法使用自定义迭代器获取std::find_if进行编译
Unable to get std::find_if to compile with custom iterator
这是导致问题的行:
auto it = find_if(menus->begin(), menus->end(), [](MenuComponent* m){return true;});
我的迭代器定义为:
class ComponentIterator : std::iterator< std::forward_iterator_tag, MenuComponent* >
定义了++
, ==
, !=
, *
, ->
。该迭代器也适用于以下行:
auto it = copy_if(menus->begin(), menus->end(), othermenu.begin(), [](MenuComponent* m){return true;});
g++ 4.8.1输出如下:
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
任何想法?
最可能的原因是class ComponentIterator : blah blah
的继承是私有的。
就像Loki在评论中说的那样,如果没有一个可用的例子来确认切换到public
是否解决了问题,很难确定。
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 我的简单if-else语句是如何无法访问的代码
- 如何将enable-if与模板参数和参数包一起使用
- 无论条件是否为true,if总是在c++中执行
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 如何删除peer if else分支中的冗长句子
- 我似乎对if/else的基本语句有问题:/
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- if数组上的随机数
- 将按位if条件转换为普通if条件
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 为什么 SearchBack 中的 if (find == A[i]) 除了第一次之外没有给出 true?在调试时,除了第一次之外显然不起作用