ตัววนซ้ำที่กำหนดเอง

ฉันได้ใช้ตัววนซ้ำเช่นนี้

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::generate และ std::find แต่เมื่อฉันใช้กับ std::max_element ฉันได้รับข้อผิดพลาด:

main.cpp: ในฟังก์ชัน 'int main(int, const char**)': main.cpp:105:16: ข้อผิดพลาด: 'iter' ไม่ได้ถูกประกาศในขอบเขตนี้ ในไฟล์ที่รวมมาจาก /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: ในฟังก์ชัน '_FIter std::max_element(_FIter , _FIter) [ด้วย _FIter = Map::GridIterator, Map::Grid*, Map::Grid&, Map::Rectangle>]': main.cpp:102:53:
สร้างอินสแตนซ์จากที่นี่ /usr/include/ c++/4.6/bits/stl_algo.h:6243:4: ข้อผิดพลาด: การใช้ฟังก์ชันที่ถูกลบ '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>&)' ในไฟล์รวมมาจาก ./ grid/Map_Grid.h:8:0 จาก main.cpp:4: ./grid/Map_GridIterator.h:17:8: ข้อผิดพลาด: 'Map::GridIterator, Map::Grid*, Map::Grid&, Map: :สี่เหลี่ยมผืนผ้า>& แผนที่::GridIterator, แผนที่::Grid*, แผนที่::Grid&, แผนที่::สี่เหลี่ยมผืนผ้า>::operator=(const Map::GridIterator, แผนที่::Grid*, แผนที่::Grid&, แผนที่:: สี่เหลี่ยมผืนผ้า>&)' ถูกลบโดยปริยายเนื่องจากคำจำกัดความเริ่มต้นจะมีรูปแบบไม่ถูกต้อง: ./grid/Map_GridIterator.h:17:8: ข้อผิดพลาด: สมาชิกอ้างอิงที่ไม่คงที่ 'Map::Grid& Map::GridIterator, Map:: Grid*, Map::Grid&, Map::Rectangle>::grid_' ไม่สามารถใช้ตัวดำเนินการกำหนดค่าเริ่มต้นได้

มีความคิดเห็นเกี่ยวกับสิ่งที่ฉันทำผิดหรือไม่?


person Q-bertsuit    schedule 07.12.2014    source แหล่งที่มา
comment
ฟังดูเป็นไปไม่ได้ที่จะตอบคุณหากไม่มีการใช้งาน บันทึกข้อผิดพลาดบ่งชี้เพียงว่ามีการดำเนินการมอบหมายสำเนาที่ผิดกฎหมาย   -  person E_net4 the curator    schedule 07.12.2014


คำตอบ (1)


ตัวดำเนินการมอบหมาย (ที่ max_element ดูเหมือนจะต้องการ) ไม่ได้สร้างขึ้นโดยค่าเริ่มต้นสำหรับตัววนซ้ำของคุณเนื่องจากสมาชิกอ้างอิง

GridT& grid_;

...ซึ่งไม่สามารถเปลี่ยนแปลงเพื่ออ้างอิงวัตถุ GridT อื่นได้ วิธีง่ายๆ วิธีหนึ่งในการแก้ไขปัญหานี้คือให้ยึดตารางไว้ตามตัวชี้แทน

person Wintermute    schedule 07.12.2014