[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

List erase iterator outside range

Krice

10/29/2008 10:21:00 AM

I get "list erase iterator outside range" message from MSVC with this
function:

int Level::Remove_Npc(Game_Object *o)
{
list <Game_Object *>::iterator pos;
Game_Object *c;

for (pos=npc_list.begin(); pos!=npc_list.end(); pos++)
{
c=(*pos);
if (c==o)
{
c->Clear_From_Map();
delete c;
item_list.erase(pos);
return 0;
}
}
return -1;
}

I have also Level::Remove_Item which operates item_list, but it seems
to be working.
It's exactly like this one. Is something wrong here?
11 Answers

Krice

10/29/2008 10:25:00 AM

0

On 29 loka, 12:20, Krice <pau...@mbnet.fi> wrote:
> item_list.erase(pos);

Eh, oops.. cut-pasted the code, but forgot to rename this one
to npc_list. Bug fixed.

Marcel Müller

10/29/2008 1:32:00 PM

0

Krice schrieb:
> I get "list erase iterator outside range" message from MSVC with this
> function:
>
> int Level::Remove_Npc(Game_Object *o)
^^^ ^^^
Approximately here I get the first bunch of compiler errors.

So please either post resonable C++ code or choose an appropriate newsgroup.


Marcel

Maxim Yegorushkin

10/29/2008 1:46:00 PM

0

On Oct 29, 1:31 pm, Marcel Müller <news.5.ma...@spamgourmet.com>
wrote:
> Krice schrieb:> I get "list erase iterator outside range" message from MSVC with this
> > function:
>
> > int Level::Remove_Npc(Game_Object *o)
>
>        ^^^               ^^^
> Approximately here I get the first bunch of compiler errors.
>
> So please either post resonable C++ code or choose an appropriate newsgroup.

This particular question does not take a rocket scientist to infer
that:
* Level is a class or a namespace name.
* Game_Object is a class with a member function Clear_From_Map().
* npc_list is a std::list<Game_Object*> object, accessible from Level.

To make it compile you add some code like that:

#include <list>

struct Game_Object
{
void Clear_From_Map();
};

struct Level
{
int Remove_Npc(Game_Object *o);
std::list<Game_Object *> npc_list;
};

using std::list;

// original piece of code follows

--
Max

jt

10/29/2008 1:50:00 PM

0

On Wed, 29 Oct 2008 03:20:41 -0700, Krice wrote:

> I get "list erase iterator outside range" message from MSVC with this
> function:
>
> int Level::Remove_Npc(Game_Object *o) {
> list <Game_Object *>::iterator pos;
> Game_Object *c;
>
> for (pos=npc_list.begin(); pos!=npc_list.end(); pos++) {
> c=(*pos);
> if (c==o)
^^^^^^^^^

if (c != 0)

> {
> c->Clear_From_Map();
> delete c;
> item_list.erase(pos);
> return 0;
> }
> }
> return -1;
> }
>
> I have also Level::Remove_Item which operates item_list, but it seems to
> be working.
> It's exactly like this one.

I doubt it...

> Is something wrong here?





--
Lionel B

Krice

10/29/2008 2:02:00 PM

0

On 29 loka, 15:31, Marcel Müller <news.5.ma...@spamgourmet.com> wrote:
> Approximately here I get the first bunch of compiler errors.

Wow, what a surprise!

> So please either post resonable C++ code

Fuck you.

Krice

10/29/2008 2:05:00 PM

0

On 29 loka, 15:50, Lionel B <m...@privacy.net> wrote:
> > if (c==o)
>
> ^^^^^^^^^
>
> if (c != 0)

You fail.

Kai-Uwe Bux

10/29/2008 2:08:00 PM

0

Lionel B wrote:

> On Wed, 29 Oct 2008 03:20:41 -0700, Krice wrote:
>
>> I get "list erase iterator outside range" message from MSVC with this
>> function:
>>
>> int Level::Remove_Npc(Game_Object *o) {
>> list <Game_Object *>::iterator pos;
>> Game_Object *c;
>>
>> for (pos=npc_list.begin(); pos!=npc_list.end(); pos++) {
>> c=(*pos);
>> if (c==o)
> ^^^^^^^^^
>
> if (c != 0)

Could it be that you missed the parameter o?

I think, the OP wants to remove exactly that pointer o from the list and
delete it.

>> {
>> c->Clear_From_Map();
>> delete c;
>> item_list.erase(pos);
>> return 0;
>> }
>> }
>> return -1;
>> }
[snip]


Best

Kai-Uwe Bux

jt

10/29/2008 3:02:00 PM

0

On Wed, 29 Oct 2008 07:05:24 -0700, Krice wrote:

> On 29 loka, 15:50, Lionel B <m...@privacy.net> wrote:
>> > if (c==o)
>>
>> ^^^^^^^^^
>>
>> if (c != 0)
>
> You fail.

Yup :-) In future I shall read posts first before responding. Might I
suggest, though, that `o' is a pretty bad choice of name for an
identifier (though I have been known to use it myself, along with `l').

Apart from that I don't see a problem in the code you post, which
suggests a bug in some other part of your code. A compileable example
demonstrating the problem would certainly help.

--
Lionel B

Krice

10/29/2008 3:19:00 PM

0

On 29 loka, 17:02, Lionel B <m...@privacy.net> wrote:
> Apart from that I don't see a problem in the code you post, which
> suggests a bug in some other part of your code.

I don't know if you can see the message I replied to myself,
but I already found the bug. I was erasing from item_list
which was wrong list, should have been npc_list.

James Kanze

10/30/2008 9:45:00 AM

0

On Oct 29, 4:02 pm, Lionel B <m...@privacy.net> wrote:
> On Wed, 29 Oct 2008 07:05:24 -0700, Krice wrote:
> > On 29 loka, 15:50, Lionel B <m...@privacy.net> wrote:

> Yup :-) In future I shall read posts first before responding.
> Might I suggest, though, that `o' is a pretty bad choice of
> name for an identifier (though I have been known to use it
> myself, along with `l').

Not as bad as O (capital):-). (I once saw a program which used
only O, l, 0, 1 and _ in symbols. The source code looked like a
binary dump. And it was a candidate for IOCCC, not production
code.)

If it makes you feel better, I misread it like you did the first
time as well.

> Apart from that I don't see a problem in the code you post,
> which suggests a bug in some other part of your code. A
> compileable example demonstrating the problem would certainly
> help.

Except that in his line:
item_list.erase(pos);
pos isn't an iterator into item_list.

It's true that if he had tried to make his example compilable,
he probably would have found the problem immediately, because he
only would have declared one list, and the compiler would have
complained "symbol not defined" for the other.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34