2014-12-07 101 views
0

我实现了像迭代这样自定义迭代器

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; 
    typedef 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; 


private: 

    GridT& grid_; 
    ShapeT<typename GridT::Resolution> shape_; 
    Index iterIndex_; 
    Index iterIndexEnd_; 

}; 

它正常工作与的std ::生成和std ::发现,但是当我的std :: max_element使用它,我得到了自己的错误:

main.cpp: In function ‘int main(int, const char**)’: main.cpp:105:16: error: ‘iter’ was not declared in this scope In file included from /usr/include/c++/4.6/algorithm:63:0, from ./grid/Map_Grid.h:11, from main.cpp:4: /usr/include/c++/4.6/bits/stl_algo.h: In function ‘_FIter std::max_element(_FIter, _FIter) [with _FIter = Map::GridIterator, Map::Grid*, Map::Grid&, Map::Rectangle>]’: main.cpp:102:53:
instantiated from here /usr/include/c++/4.6/bits/stl_algo.h:6243:4: error: use of deleted function ‘Map::GridIterator, Map::Grid*, Map::Grid&, Map::Rectangle>& Map::GridIterator, Map::Grid*, Map::Grid&, Map::Rectangle>::operator=(const Map::GridIterator, Map::Grid*, Map::Grid&, Map::Rectangle>&)’ In file included from ./grid/Map_Grid.h:8:0, from main.cpp:4: ./grid/Map_GridIterator.h:17:8: error: ‘Map::GridIterator, Map::Grid*, Map::Grid&, Map::Rectangle>& Map::GridIterator, Map::Grid*, Map::Grid&, Map::Rectangle>::operator=(const Map::GridIterator, Map::Grid*, Map::Grid&, Map::Rectangle>&)’ is implicitly deleted because the default definition would be ill-formed: ./grid/Map_GridIterator.h:17:8: error: non-static reference member ‘Map::Grid& Map::GridIterator, Map::Grid*, Map::Grid&, Map::Rectangle>::grid_’, can’t use default assignment operator

有关我在做什么错的任何想法?

+0

这听起来不可能在没有实施的情况下回答你。错误日志仅表明执行了非法复制分配。 – 2014-12-07 01:55:48

回答

0

赋值运算符(即max_element似乎需要)没有针对你的迭代器默认生成的,因为参考构件

GridT& grid_; 

...这不能改变引用不同GridT对象。解决这个问题的一个简单方法是用指针保持网格。