2016-02-01 66 views
0

我正在爲Queue數據類型實現迭代器,但由於某種原因,迭代器正在作爲類實現內部的常量初始化。我無法弄清楚爲什麼構造函數會導致迭代器以const形式返回自身。迭代器實現 - (鏈接列表隊列)

任何有關可能導致我的問題的C++語言錯綜複雜的反饋都會非常有幫助。

我從Eclipse的接收,這似乎是從我開始()方法來錯誤是:

../src/linked_queue.hpp:315:35: error: invalid conversion from 
    'const ics::LinkedQueue<int>*' to 'ics::LinkedQueue<int>*' [-fpermissive] 

接口:

#ifndef LINKED_QUEUE_HPP_ 
#define LINKED_QUEUE_HPP_ 

#include <string> 
#include <iostream> 
#include <sstream> 
#include <initializer_list> 
#include "ics_exceptions.hpp" 


namespace ics { 


template<class T> class LinkedQueue { 
    public: 
    //Destructor/Constructors 
    ~LinkedQueue(); 

    LinkedQueue   (); 
    LinkedQueue   (const LinkedQueue<T>& to_copy); 
    explicit LinkedQueue (const std::initializer_list<T>& il); 


    template <class Iterable> 
    explicit LinkedQueue (const Iterable& i); 


    //Queries 
    bool empty  () const; 
    int size  () const; 
    T& peek  () const; 
    std::string str() const; //supplies useful debugging information; contrast to operator << 


    //Commands 
    int enqueue (const T& element); 
    T dequeue(); 
    void clear (); 

    template <class Iterable> 
    int enqueue_all (const Iterable& i); 


    //Operators 
    LinkedQueue<T>& operator = (const LinkedQueue<T>& rhs); 
    bool operator == (const LinkedQueue<T>& rhs) const; 
    bool operator != (const LinkedQueue<T>& rhs) const; 

    template<class T2> 
    friend std::ostream& operator << (std::ostream& outs, const LinkedQueue<T2>& q); 



    private: 
    class LN; 

    public: 
    class Iterator { 
     public: 

     ~Iterator(); 
     T   erase(); 
     std::string str () const; 
     LinkedQueue<T>::Iterator& operator ++(); 
     LinkedQueue<T>::Iterator operator ++ (int); 
     bool operator == (const LinkedQueue<T>::Iterator& rhs) const; 
     bool operator != (const LinkedQueue<T>::Iterator& rhs) const; 
     T& operator * () const; 
     T* operator ->() const; 
     friend std::ostream& operator << (std::ostream& outs, const LinkedQueue<T>::Iterator& i) { 
      outs << i.str(); 
      return outs; 
     } 
     friend Iterator LinkedQueue<T>::begin() const; 
     friend Iterator LinkedQueue<T>::end () const; 

     private: 

     LN*    prev = nullptr; 
     LN*    current;   
     LinkedQueue<T>* ref_queue; 
     int    expected_mod_count; 
     bool   can_erase = true; 

     Iterator(LinkedQueue<T>* iterate_over, LN* initial); 
    }; 


    Iterator begin() const; 
    Iterator end () const; 


    private: 
    class LN { 
     public: 
     LN()      {} 
     LN (const LN& ln)   : value(ln.value), next(ln.next){} 
     LN (T v, LN* n = nullptr) : value(v), next(n){} 

     T value; 
     LN* next = nullptr; 
    }; 


    LN* front  = nullptr; 
    LN* rear  = nullptr; 
    int used  = 0;   //Cache for number of values in linked list 
    int mod_count = 0;   //For sensing any concurrent modifications 

    //Helper methods 
    void delete_list(LN*& front); 
}; 

實施(我只包括的部分我的迭代器代碼):

template<class T> 
auto LinkedQueue<T>::begin() const -> LinkedQueue<T>::Iterator { 
    return Iterator(this, this->front); 
} 

template<class T> 
auto LinkedQueue<T>::end() const -> LinkedQueue<T>::Iterator { 
    // return Iterator(this, this->rear); 
} 

template<class T> 
LinkedQueue<T>::Iterator::Iterator(LinkedQueue<T>* iterate_over, LN* initial) { 
    ref_queue = iterate_over; 
    expected_mod_count = iterate_over->mod_count; 
    current = initial; 
} 
+1

'Iterator begin()const;'被標記爲'const',因此函數中使用的'this'也是'const'。所以編譯器會提供一個錯誤,即它無法從常量轉換爲非常量。 – Niall

+0

可以理解。但是我將這個作爲一個類來使用,並且我應該以這種方式實現函數,那麼是否有任何方法可以正確地實現Iterator/begin函數,以便我可以將引用傳遞給Iterator? – Cameron

+1

你可以在構造函數簽名'Iterator(LinkedQueue const * iterate_over,LN * initial);'和成員const以及'LinkedQueue const * ref_queue;'' – Niall

回答

1

錯誤是因爲Iterator begin() const;被標記爲const,因此函數中使用的this也是const。所以編譯器會提供一個錯誤,即它無法從常量轉換爲非常量。

鑑於功能簽名在這種情況下是固定的,爲了解決該錯誤,添加const將有助於解決該問題。

你可以添加const到構造函數簽名

Iterator(LinkedQueue<T> const* iterate_over, LN* initial); 

,使成員const以及

LinkedQueue<T> const* ref_queue; 

添加const在需要確保部件及功能仍然const的要求是已知的爲being const correct