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

...我无法修改_current

我如何使我的迭代器现在像 a 一样工作,const_iterator以便从我的链表中我可以调用获取 and 的 const 版本begin()end()以及cbegin()and cend()

4

1 回答 1

2

正确的。问题是您声明 const_iterator typedef。(请参阅如何正确实现自定义迭代器和 const_iterators?

代替

typename typedef list_iterator<T_> const  const_iterator;

你要

typename typedef list_iterator<const  T_> const_iterator;
于 2014-02-19T19:59:16.137 回答