List erase iterator outside range

K

Krice

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?
 
M

Marcel Müller

Krice said:
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
 
M

Maxim Yegorushkin

Krice schrieb:> I get "list erase iterator outside range" message from MSVC with this


       ^^^               ^^^
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
 
L

Lionel B

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...
 
K

Kai-Uwe Bux

Lionel said:
^^^^^^^^^

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.
[snip]


Best

Kai-Uwe Bux
 
L

Lionel B

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.
 
K

Krice

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.
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top