List iterator not incrementable error message in C++

11,614

Your call to list1.pop_front() removes the element which the iterator is pointing to initially, invalidating it. And an invalid iterator can not be incremented. :)

It took a few minutes to find with the debugger. Just keep an eye on the value of 'it' as you step through the program. I don't know if you know how to use the debugger yet, but if not, do yourself a favor and learn it. It's an invaluable tool.

(By the way, in the future, please be explicit about whether the error occurs at compile-time or while running the program. Your question stated the error occurred when "compiling the program". I just edited the question for you, hope you don't mind. But it's an important distinction, and makes it harder to answer your question accurately)

Share:
11,614

Related videos on Youtube

josesuero
Author by

josesuero

Meh. Disenfranchised SO user. Fed up with the elitism and condescension and the Lords of the Flies nature of the SE network. Wouldn't it be nice if we were all a bit nicer to each others?

Updated on April 19, 2022

Comments

  • josesuero
    josesuero about 2 years

    As a newbie I'm trying to implement a sorting function in C++, using the list-class. However, running the code I get the error that the list iterator is not incrementable... However it seems very unlikely as it should be incrementable!

    code:

    void shuffle (list<int> &list1)
    {
        list<int> smaller;
        list<int> larger;
    
        if (list1.size() > 1)
        {
            list<int>::iterator it;
            //int it;
    
            int x = list1.front();
    
    
            for (it = list1.begin(); it != list1.end(); it++)
            {                                       
                if(*it <= x)
                {
                    smaller.push_front(*it);
                    list1.pop_front();
    
                }
                else
                {
                    larger.push_back(*it);
                    list1.pop_front();
                }
                shuffle (smaller);
                shuffle (larger);
            }
        }
        else
        {
            print(smaller);
            print(larger);
    
            //cout << "No sorting needed! The list still looks like: ";
            //print(list1);
        }
        print(smaller);     
        print(larger);
    }
    

    I implemented this function just in de CPP file, under the main.

    Does anyone has any suggestions?