Q-b*_*uit 1 c++ iterator stl 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;
....
Run Code Online (Sandbox Code Playgroud)
}
使用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 = 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>&)'/ usr /include/c++/4.6/bits/stl_algo.h:4404:45:注意:候选者是:/usr/include/c++/4.6/bits/ stl_iterator_base_types.h:202:5:注意:模板typename 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 = Map :: GridIterator,Map :: Grid*,Map :: Grid&,Map :: Rectangle> ]':main.cpp:102:60:
从这里实例化/usr/include/c++/4.6/bits/stl_algobase.h:215:7:错误:'__ <_ b'/ usr中的'operator <'不匹配/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:note:te mplate bool std :: operator <(const std :: reverse_iterator <_IteratorL>&,const std :: reverse_iterator <_IteratorR>&)/usr/include/c++/4.6/bits/stl_iterator.h:1049:5:注意:模板bool std :: operator <(const std :: move_iterator <_IteratorL>&,const std :: move_iterator <_IteratorR>&)/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:note:template 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:注意:模板bool std :: operator <(const std :: basic_string <_CharT,_Traits,_Alloc>&,const _CharT*)/usr/include/c++/4.6/bits/basic_string.h:2534:5:note :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 ::操作符< (const std :: vector <_Tp,_Alloc>&,const std :: vector <_Tp,_Alloc>&)/usr/include/c++/4.6/tuple:586:5:注意:模板bool std :: operator <( const std :: tuple <_TElements ...>&,const std :: tuple <_Elements ...>&)
您缺少一个typedef关键字,用于声明指示迭代器类别的别名:
// Iterator traits - typedefs and types required to be STL compliant
//...
typedef std::forward_iterator_tag iterator_category;
~~~~~~^
Run Code Online (Sandbox Code Playgroud)
没有typedef,你实际上是在声明一个数据成员.
为避免此类错误,您可以将std::iterator类模板用作基类,而不是自己定义这些别名:
class GridIterator : public std::iterator<std::forward_iterator_tag
, typename GridT::Element>
Run Code Online (Sandbox Code Playgroud)