2014-02-19 66 views
1

我已經實現了一個雙向鏈表,並創建了一個迭代器,它可以擴展爲std::iterator。我正在嘗試創建const版本。鏈接列表常量迭代器

我想:

typename typedef list_iterator<T_>  iterator; 
typename typedef list_iterator<T_> const const_iterator; 

如果我這樣做,雖然,我得到這個錯誤:

error C2678: binary '--' : no operator found which takes a left-hand operand of type 'const list_iterator<T_>' (or there is no acceptable conversion) 

這裏是我的operator--

list_iterator& operator --() 
{ 
    _current = _current->_previous; 
    return *this; 
} 

list_iterator operator--(int) // postfix 
{ 
    list_iterator hold = *this; 
    --*this; 
    return list_iterator(hold); 
} 

如果我把

list_iterator operator--() const 

......我不能怎樣做,現在我的迭代器就像一個const_iterator工作,以便從我的鏈表我可以打電話得到的begin()end() const版本修改的_current

值,以及cbegin()cend()

+0

嘗試宣告_current可變 – segfault

+0

一個'const_iterator'和'常量iterator'是_very_不同的事情。他們實際上需要兩種不同的類型,對不起。這是「數據常量指針」和「常量數據指針」之間的區別 –

+0

所以我需要創建一個全新的迭代器類呢? – user2120910

回答