-
May 22nd, 2011, 08:28 AM
#1
[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
-
May 22nd, 2011, 08:47 AM
#2
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.
-
May 22nd, 2011, 09:20 AM
#3
Re: [STL] list iterator not incrementable
Originally Posted by laserlight
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");
}
-
May 22nd, 2011, 10:20 AM
#4
Re: [STL] list iterator not incrementable
Originally Posted by Goldstein
liste.h
First you should use code tags when posting code. The code you posted is practically unreadable.
Regards,
Paul McKenzie
-
May 22nd, 2011, 10:29 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|