Skip to main content
gamedev.net
🔒 Locked

creating iterator for a struct

Started by mental_damage_2004 May 1, 2004 at 9:27 PM 2 replies 250+ views
Original Post
mental_damage_2004
mental_damage_2004
I''m doing an app in c++ and i''ve run into a problem , here is my code:
struct mything
{
	list counter;
	list thing2;
}category;
	
list categories;

list::iterator counter; //causes the program to crash
 
as you can see creating an iterator for the struc category causes my app to crash with the message: "Program received signal SIGSEGV, Segmentation fault." (working under gentoo linux). Obviously something is wrong with my code...can anybody help me fix my mistake(s)?
Monder
Monder
Well seeing as he said it causes a segfault it must be a run-time crash. But the code you've posted ain't valid C++ (unless you've done something weird or the list type you have there is not an std::list). Is that the actual code from your source file or just a psuedo-code version?

[edited by - Monder on May 2, 2004 7:05:40 AM]
Aprosenf
Aprosenf
The board ate all of the < and > tags. The code mental_damage_2004 tried to post was:

struct mything
{ list<int> counter;
list<string> thing2;
}category;

list<mything> categories;
list<mything>::iterator counter; //causes the program to crash


Where exactly does the app crash? You're probably trying to dereference an invalid iterator. To iterate through the list, do the following:

for(counter = categories.begin(); counter != categories.end(); ++counter)

Also, sometimes it's good to check if the list is empty before doing stuff. To do that, call categories.empty(), which returns a bool, true if it is empty, false if it is not empty.

[edited by - aprosenf on May 2, 2004 10:58:16 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.