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

时间:2014-12-06 18:54:34

标签: c++ stl iterator stl-algorithm

我正在尝试创建自己的迭代器,并且我已经使用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&amp;)[with _IIter =   Map :: GridIterator,Map :: Grid *,   Map :: Grid&amp;,Map :: Rectangle&gt;,_ Tp = int]':   main.cpp:103:50:从这里实例化   /usr/include/c++/4.6/bits/stl_algo.h:4404:45:错误:没有匹配   呼叫功能   “__iterator_category(地图:: GridIterator,   Map :: Grid *,Map :: Grid&amp;,Map :: Rectangle&gt;&amp;)'   /usr/include/c++/4.6/bits/stl_algo.h:4404:45:注意:候选人是:   /usr/include/c++/4.6/bits/stl_iterator_base_types.h:202:5:注意:   template typename std :: iterator_traits :: iterator_category   std :: __ iterator_category(const _Iter&amp;)

使用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&amp;   std :: max(const _Tp&amp;,const _Tp&amp;)[with _Tp =   Map :: GridIterator,Map :: Grid *,   Map :: Grid&amp;,Map :: Rectangle&gt;]':main.cpp:102:60:
  从这里实例化/usr/include/c++/4.6/bits/stl_algobase.h:215:7:   错误:'__a&lt;中的'运算符&lt;'不匹配__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&lt;(const std :: pair&lt; _T1,_T2&gt;&amp ;,   const std :: pair&lt; _T1,_T2&gt;&amp;)   /usr/include/c++/4.6/bits/stl_iterator.h:291:5:注意:模板bool std :: operator&lt;(const std :: reverse_iterator&lt; _Iterator&gt;&amp;,const   的std :: reverse_iterator的&LT; _Iterator&GT;&安培)   /usr/include/c++/4.6/bits/stl_iterator.h:341:5:注意:模板bool std :: operator&lt;(const std :: reverse_iterator&lt; _IteratorL&gt;&amp;,const   的std :: reverse_iterator的&LT; _IteratorR&GT;&安培)   /usr/include/c++/4.6/bits/stl_iterator.h:1049:5:注意:模板bool std :: operator&lt;(const std :: move_iterator&lt; _IteratorL&gt;&amp;,const   的std :: move_iterator&LT; _IteratorR&GT;&安培)   /usr/include/c++/4.6/bits/stl_iterator.h:1055:5:注意:模板bool std :: operator&lt;(const std :: move_iterator&lt; _Iterator&gt;&amp;,const std :: move_iterator&lt; _Iterator&gt;&amp; )   /usr/include/c++/4.6/bits/basic_string.h:2510:5:注意:模板bool std :: operator&lt;(const std :: basic_string&lt; _CharT,_Traits,_Alloc&gt;&amp;,const   std :: basic_string&lt; _CharT,_Traits,_Alloc&gt;&amp;)   /usr/include/c++/4.6/bits/basic_string.h:2522:5:注意:模板bool std :: operator&lt;(const std :: basic_string&lt; _CharT,_Traits,_Alloc&gt;&amp;,const _CharT *)   /usr/include/c++/4.6/bits/basic_string.h:2534:5:注意:模板bool std :: operator&lt;(const _CharT *,const std :: basic_string&lt; _CharT,_Traits,_Alloc&gt;&amp;)/ usr /include/c++/4.6/bits/stl_vector.h:1290:5:注意:模板bool std :: operator&lt;(const std :: vector&lt; _Tp,_Alloc&gt;&amp;,const std :: vector&lt; _Tp,_Alloc&gt; &amp;)/usr/include/c++/4.6/tuple:586:5:注意:模板bool std :: operator&lt;(const std :: tuple&lt; _TElements   ...&gt;&amp;,const std :: tuple&lt; _Elements ...&gt;&amp;)

1 个答案:

答案 0 :(得分:5)

您缺少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>