1

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()?

David G
  • 94,763
  • 41
  • 167
  • 253
user2120910
  • 404
  • 1
  • 5
  • 16

1 Answers1

2

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;
Community
  • 1
  • 1
m24p
  • 664
  • 4
  • 12