提问者:小点点

在stl算法中使用我的自定义迭代器


我正在尝试创建我自己的迭代器,并且我已经使它在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/Algorith:63:0,from./grid/map_grid.h:11,from main.cpp:4:/usr/include/C++/4.6/bits/stl_algo.h:在函数'_iiterstd::find(_iiter,_iiter,const_tp&)中[with_IITER=MAP::Griditerator,MAP::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>&)'没有匹配函数

使用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/ostream:40,/./grid/map_griditerator.h:7,/./grid/map_grid.h:8,from main.cpp:4:/usr/include/c++/4.6/bits/stl_algobase.h:在函数'const_tp&;STD::MAX(const_tp&;,const_tp&;)[with_TP=MAP::GridIterator,MAP::Grid*,MAP::Grid&;,MAP::Rectangle>]“:main.cpp:102:60:
从此处实例化/usr/include/C++/4.6/bits/stl_algobase.h:215:7:错误:”__a<;__B'/usr/include/C++/4.6/bits/stl_algobase.H:215:7:注意:候选项是:/usr/include/C++/4.6/bits/stl_pair.H:207:5:注意:模板constexpr bool std::operator<(const std::pair<_t1,_t2>&,const std::pair<_t1,_t2>&)/usr/include/c++/4.6/bits/stl_iterator.h:291:5:注意:模板bool std::operator<(const std::reverse_iterator<_iterator>>,const std::reverse_iterator<_iterator>)/usr/include/c++/4.6/bits/stl_iterator.h:341:5:注意:template bool std::operator<(const std::reverse_iterator<_iterator>&,const std::reverse_iterator<_iterator&>)/usr/include/c++/4.6/bits/stl_iterator.h:1049:5:注意:template bool std::operator<(const std::move_iterator<_iterator>&,const std::move_iterator<_iterator&>)/usr/include/c++/4.6/bits/stl_iterator.h:1055:5:注意:模板bool std::operator<(const std::move_iterator<_iterator>>,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>&,const std::basic_string<_chart,_traits,_alloc>)/usr/include/c++/4.6/bits/basic_string.h:2522:5:注意:template bool std::operator<(const std::basic_string<_chart,_traits,_alloc>&,const_chart*)/usr/include/c++/4.6/bits/basic_string.h:2534:5:注意:template bool std::operator<(const_chart*,const std::basic_string<_chart,_traits,_alloc>&)/usr/include/C++/4.6/bits/stl_vector.h:1290:5:注意:模板bool std::operator<(const std::vector<_tp,_alloc>&,const std::vector<_tp,_alloc>&)/usr/include/C++/4.6/tuple:586:5:注意:template bool std::operator<(const std::tuple<_telements...>&,const std::tuple<_elements...>&)


共1个答案

匿名用户

缺少用于声明指示迭代器类别的别名的typedef关键字:

// Iterator traits - typedefs and types required to be STL compliant
//...
typedef std::forward_iterator_tag iterator_category;
~~~~~~^

如果没有typedef,您实际上是在声明一个数据成员。

为了避免此类错误,您可以使用std::iterator类模板作为基类,而不是自己定义这些别名:

class GridIterator : public std::iterator<std::forward_iterator_tag
                                        , typename GridT::Element>