473,471 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

List erase iterator outside range

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?
Oct 29 '08 #1
9 6685
On 29 loka, 12:20, Krice <pau...@mbnet.fiwrote:
item_list.erase(pos);
Eh, oops.. cut-pasted the code, but forgot to rename this one
to npc_list. Bug fixed.

Oct 29 '08 #2
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
Oct 29 '08 #3
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
Oct 29 '08 #4
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
Oct 29 '08 #5
On 29 loka, 15:50, Lionel B <m...@privacy.netwrote:
if (c==o)

^^^^^^^^^

if (c != 0)
You fail.
Oct 29 '08 #6
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
Oct 29 '08 #7
On Wed, 29 Oct 2008 07:05:24 -0700, Krice wrote:
On 29 loka, 15:50, Lionel B <m...@privacy.netwrote:
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
Oct 29 '08 #8
On 29 loka, 17:02, Lionel B <m...@privacy.netwrote:
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.
Oct 29 '08 #9
On Oct 29, 4:02 pm, Lionel B <m...@privacy.netwrote:
On Wed, 29 Oct 2008 07:05:24 -0700, Krice wrote:
On 29 loka, 15:50, Lionel B <m...@privacy.netwrote:
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:ja*********@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

Oct 30 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Brett L. Moore | last post by:
Hi, I have had trouble determining whether the STL list.size() operation is O(1) or O(n). I know the list is a doubly-linked list, so if the size() operation begins at the head, then counts to...
2
by: s | last post by:
Here is a snippet of my code: <code> list< MyClass * >outgoing_pool; MyClass* GetWaitingObject(string freq) { MyClass *temp_ptr = NULL; list<MyClass*>::iterator i;
8
by: ma740988 | last post by:
Consider: # include <iostream> using std::cout; using std::cin; using std::endl; # include <list> using std::list;
12
by: Christoff Pale | last post by:
Hi, suppose I have a list of strings; I have to iterate of the strings and delete certain entries. I and not sure how to do this? for example: #include<list> #include<string>...
8
by: olanglois | last post by:
Hi, I was asking myself to following question. What is better to erase an element from a STL map: calling (option #1) size_type erase(const key_type& k) or calling (option #2)
3
by: janzon | last post by:
Hi! Sorry for the bad subject line... Here's what I mean. Suppose we deal with C++ standard integers lists (the type is indifferent). We have a function f, declared as list<intf(int); Now...
6
by: catphive.lists | last post by:
Is there a way to call erase(iter) on a list without invalidating the iterator? Can I make a copy of an iterator and then move forward the original without moving the copy? I'm aware of the...
3
by: JurgenvonOerthel | last post by:
I want to replace one element in a list<stringby a list<stringand I need to obtain an iterator to the first element in the inserted list. In code: void...
6
by: APEJMAN | last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result I post the code here I dont wanne bother you, but if any one of you have time to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.