[STL] list iterator not incrementable
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [STL] list iterator not incrementable

  1. #1
    Join Date
    May 2011
    Posts
    3

    [STL] list iterator not incrementable

    Hey everybody,

    i hope my question wasnt mentionned in FAQ:

    Ok, i'm working on a very simple project but when it comes to implementing the methode advance to increment the iterator current, it doesnt work and says list iterator not incrementable, heres the code:

    void liste::avancer()
    {

    courant++;

    }

    and heres the constructor one:

    liste::liste(void)
    {
    courant = maliste.begin();
    }

    when i call the 'avancer' method from the main i have an error: list iterator not incrementable

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,246

    Re: [STL] list iterator not incrementable

    It might be a good idea to post the smallest and simplest program that you expect to compile, but which demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2011
    Posts
    3

    Re: [STL] list iterator not incrementable

    Quote Originally Posted by laserlight View Post
    It might be a good idea to post the smallest and simplest program that you expect to compile, but which demonstrates the problem.
    liste.h

    #pragma once
    #include<list>
    #include<iostream>
    using namespace std;

    class liste
    {
    list<int> maliste;
    list<int>::iterator courant;
    public:
    liste(void);
    void inserer();
    void avancer();
    void show();
    ~liste(void);
    };

    liste.cpp

    #include "liste.h"
    #include<iterator>


    liste::liste(void)
    {
    courant = maliste.begin(); // to set the pointer on the begining of my list
    }


    liste::~liste(void)
    {
    }

    void liste::show()
    {
    for (courant=maliste.begin(); courant!=maliste.end(); courant++) // to show my list
    cout << " " << *courant ;


    }

    void liste::inserer()
    {
    insert_iterator<list<int>> it(maliste,maliste.begin()); // to insert an element
    *it = 2;
    }

    void liste::avancer()
    {

    advance(courant,1); // or courant++ : both methods dont work!

    }


    run.cpp

    #include"liste.h"

    void main ()
    {
    liste a;
    a.inserer(); // i insert 2 times the value 2
    a.inserer();
    a.avancer(); // here i want to increment the current iterator but it doesnt work
    system("pause");
    }

  4. #4
    Join Date
    Apr 1999
    Posts
    27,423

    Re: [STL] list iterator not incrementable

    Quote Originally Posted by Goldstein View Post
    liste.h
    First you should use code tags when posting code. The code you posted is practically unreadable.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 1999
    Posts
    27,423

    Re: [STL] list iterator not incrementable

    Code:
    liste::liste(void)
    {
       courant = maliste.begin(); // to set the pointer on the begining of my list
    }
    There are no items in the list, so this iterator is the same as maliste.end(). Therefore, this iterator cannot br dereferenced, advanced, etc.
    Code:
    void liste::avancer()
    {
        advance(courant,1); // or courant++ : both methods dont work!
    }
    You are now incrementing an invalid iterator, since it was set to end(), and you can't advance past end().

    Also:

    1) Do not use "using namespace std" in a header file.

    2) The main() function returns int, not void.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center