user2120910
user2120910

Reputation: 404

Linked list const iterator

I have implemented a doubly-linked list, and created an iterator which extends std::iterator. I'm now trying to create a const version.

I tried:

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

If I do this though, I get this error:

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

Here is my operator--:

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

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

If I put

list_iterator operator--() const

... I'm not able to modify the value of _current

How do I make my iterator now work like a const_iterator so that from my linked list I can call get the const version of begin() and end(), as well as cbegin() and cend()?

Upvotes: 1

Views: 989

Answers (1)

m24p
m24p

Reputation: 674

Right. The problem is your declaration of your const_iterator typedef. (See How to correctly implement custom iterators and const_iterators? )

Instead of

typename typedef list_iterator<T_> const  const_iterator;

you want

typename typedef list_iterator<const  T_> const_iterator;

Upvotes: 2

Related Questions