iterator not dereferencable error

1
2
3
4
5
6
7
8
9
for ( fileVector_t::iterator i = filev.begin(); i != filev.end(); ++i ){
    cout << "Extension: " << i->extension_ << "\n " << "Size: " << i->size_ << endl;
    unsigned filenum=1;
    fileVector_t::iterator iter = i;
    if( i == filev.end() )
        break;
    ++iter;
    if (iter->extension_ == i->extension_)
        cout << "same" << endl;


I'm getting an iterator not dereferencable error, because of the ++iter line I assume. Everything runs smoothly if I don't increment the iterator by taking that line out. I don't understand why this won't work, I test to see if it's equal to end(); before I increment so it will never be past the end of the vector.
if it's == end then you're already passed the end of the buffer.

Notice how your loop already checks i against end(). Therefore your if statement on line 5 effectively does nothing (it will always be false)

What you need to do is check iter+1 against end:

1
2
3
4
    fileVector_t::iterator iter = i;
    ++iter;
    if( iter == filev.end() )
        break;

that makes so much sense. That's what I was trying to say but for the life of me I couldn't express it in my code, I tried soo many different things haha thank you.
Topic archived. No new replies allowed.