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

3
  • try declaring _current mutable Commented Feb 19, 2014 at 19:56
  • a const_iterator and a const iterator are VERY different things. They actually need to be two different types, sorry. It's the difference between "a constant pointer to data", and "a pointer to constant data" Commented Feb 19, 2014 at 19:59
  • So I need to make a whole new iterator class then? Commented Feb 19, 2014 at 20:05

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.