【发布时间】:2015-02-04 18:19:39
【问题描述】:
我正在尝试创建自己的迭代器,并且使用 std::generate 算法使其按预期工作。但是,当我尝试 std::find 的 std::max_element 时,我得到了一些神秘的错误。
这是我的迭代器的接口:
template <typename GridT,
typename GridPtr,
typename GridRef,
template <typename> class ShapeT>
class GridIterator
{
public:
typedef GridIterator<GridT, GridPtr, GridRef, ShapeT> Iterator;
// Iterator traits - typedefs and types required to be STL compliant
typedef std::ptrdiff_t difference_type;
typedef typename GridT::Element value_type;
typedef typename GridT::Element* pointer;
typedef typename GridT::Element& reference;
typedef size_t size_type;
std::forward_iterator_tag iterator_category;
GridIterator(GridT& grid,
ShapeT<typename GridT::Resolution> shape,
Index iterStartIndex);
~GridIterator();
Iterator& operator++();
Iterator operator++(int);
typename GridT::Element& operator*();
typename GridT::Element* operator->();
bool operator!=(const GridIterator& rhs) const;
bool operator==(const GridIterator& rhs) const;
....
}
使用 std::find,我得到这个错误
在 /usr/include/c++/4.6/algorithm:63:0 包含的文件中, 来自./grid/Map_Grid.h:11, 从 main.cpp:4: /usr/include/c++/4.6/bits/stl_algo.h: 在函数'_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = 地图::GridIterator, 地图::Grid*, Map::Grid&, Map::Rectangle>, _Tp = int]’: main.cpp:103:50:从这里实例化 /usr/include/c++/4.6/bits/stl_algo.h:4404:45:错误:没有匹配 调用函数 '__iterator_category(Map::GridIterator, Map::Grid*, Map::Grid&, Map::Rectangle>&)’ /usr/include/c++/4.6/bits/stl_algo.h:4404:45:注意:候选人是: /usr/include/c++/4.6/bits/stl_iterator_base_types.h:202:5:注意: 模板类型名 std::iterator_traits::iterator_category std::__iterator_category(const _Iter&)
使用 std::max_element :
在 /usr/include/c++/4.6/bits/char_traits.h:41:0 包含的文件中, 来自 /usr/include/c++/4.6/ios:41, 来自 /usr/include/c++/4.6/ostream:40, 来自 /usr/include/c++/4.6/iostream:40, 来自./grid/Map_GridIterator.h:7, 来自./grid/Map_Grid.h:8, 来自 main.cpp:4: /usr/include/c++/4.6/bits/stl_algobase.h: 在函数‘const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = 地图::GridIterator, 地图::Grid*, Map::Grid&, Map::Rectangle>]': main.cpp:102:60:
从这里/usr/include/c++/4.6/bits/stl_algobase.h:215:7 实例化: 错误:‘__a &, 常量 std::pair<_t1 _t2>&) /usr/include/c++/4.6/bits/stl_iterator.h:291:5: 注意:模板 bool std::operator&, const std::reverse_iterator<_iterator>&) /usr/include/c++/4.6/bits/stl_iterator.h:341:5: 注意:模板 bool std::operator&, const std::reverse_iterator<_iteratorr>&) /usr/include/c++/4.6/bits/stl_iterator.h:1049:5: 注意:模板 bool std::operator&, const std::move_iterator<_iteratorr>&) /usr/include/c++/4.6/bits/stl_iterator.h:1055:5: 注意:模板 bool std::operator&, const std::move_iterator<_iterator>&) /usr/include/c++/4.6/bits/basic_string.h:2510:5: 注意:模板 bool std::operator&, const std::basic_string<_chart _traits _alloc>&) /usr/include/c++/4.6/bits/basic_string.h:2522:5: 注意:模板 bool std::operator&, const _CharT*) /usr/include/c++/4.6/bits/basic_string.h:2534:5: 注意:模板 bool std::operator&) /usr/ include/c++/4.6/bits/stl_vector.h:1290:5: 注意:模板 bool std::operator&, const std::vector<_tp _alloc>&) /usr/include/c++/4.6/tuple:586:5: 注意:模板 bool std::operator&, const std::tuple<_elements ...>&)
【问题讨论】:
标签: c++ templates stl iterator stl-algorithm