L3GabeL4
|
  |
| Joined: 18 Feb 2012 |
| Total Posts: 808 |
|
|
| 11 Aug 2012 07:50 PM |
pastebin /RYv4zVMZ
The problem is within the while loop in main. The object is not being removed from the list when the bunny dies. I'm getting an ACCESS_VIOLATION and I assume this has to do with the iterator not being 'updated' and trying to access the deleted object. I'm not sure I know how to fix this and Stackoverflow didn't provide any clear answer.
Help? |
|
|
| Report Abuse |
|
|
L3GabeL4
|
  |
| Joined: 18 Feb 2012 |
| Total Posts: 808 |
|
| |
|
| |
|
L3GabeL4
|
  |
| Joined: 18 Feb 2012 |
| Total Posts: 808 |
|
|
| 11 Aug 2012 08:33 PM |
| You just won't help me because you don't understand C++. |
|
|
| Report Abuse |
|
|
raikou6
|
  |
| Joined: 23 Jan 2011 |
| Total Posts: 48 |
|
|
| 11 Aug 2012 08:34 PM |
no bcuz we r lazy
gosh
man
blah |
|
|
| Report Abuse |
|
|
|
| 11 Aug 2012 08:45 PM |
| If you are getting errors on your grad project, then ask someone else to fix it, I hope you fail. |
|
|
| Report Abuse |
|
|
L3GabeL4
|
  |
| Joined: 18 Feb 2012 |
| Total Posts: 808 |
|
|
| 11 Aug 2012 08:47 PM |
| There is no error, it's an access violation. And no, this is the C++ tutorial graduation project. I'm only 13, they would not allow me in any college. |
|
|
| Report Abuse |
|
|
| |
|
L3GabeL4
|
  |
| Joined: 18 Feb 2012 |
| Total Posts: 808 |
|
|
| 11 Aug 2012 08:50 PM |
No wonder you guys just troll; you know nothing. >_> I guess that's what I should just expect.
Besides, I fixed it already. erase returns an iterator to the following object, so you can just assign that to iter. Thanks for nothing. |
|
|
| Report Abuse |
|
|
|
| 11 Aug 2012 08:56 PM |
@ROBLOX you going to complain that im 11? |
|
|
| Report Abuse |
|
|
L3GabeL4
|
  |
| Joined: 18 Feb 2012 |
| Total Posts: 808 |
|
| |
|
|
| 11 Aug 2012 09:32 PM |
Lol, he is?
That is like racism, I'm also mexican. |
|
|
| Report Abuse |
|
|
L3GabeL4
|
  |
| Joined: 18 Feb 2012 |
| Total Posts: 808 |
|
| |
|
| |
|
L3GabeL4
|
  |
| Joined: 18 Feb 2012 |
| Total Posts: 808 |
|
|
| 11 Aug 2012 09:35 PM |
| awh sorcus! you got me you amazingly hotttattat troll. |
|
|
| Report Abuse |
|
|
|
| 11 Aug 2012 10:01 PM |
First of all, while not related to your problem...
(*iter).newDay();
There's a cleaner way to do this.
iter->newDay();
Anyways, I used Microsoft's handy debugger from Visual Studio to step through the code to find your problem. Since for loops increment before they evaluate whether to keep running, and since the iterator ends up pointing to the end of the list, attempting to increment it returns an error. The way to fix this is to simply add a check after the line where you erase it from the list as to whether or not you've reached the end of the list, and break out of the loop if you have.
Also, as a note, you never add any more rabbits to the list later on despite creating functions for it, there's only that one, and when it dies it gets kinda boring. |
|
|
| Report Abuse |
|
|
|
| 11 Aug 2012 11:41 PM |
Access violations are bugs bro. Since you are the same age as me, and since it is not a crucial exam/final, I will compile the code tomorrow and fix the error. |
|
|
| Report Abuse |
|
|
|
| 12 Aug 2012 12:03 AM |
| @Chickenman: I already posted the solution. It's a problem with a lack of bounds checking before continuing the loop. |
|
|
| Report Abuse |
|
|
Tarabukka
|
  |
| Joined: 18 Jan 2011 |
| Total Posts: 394 |
|
|
| 12 Aug 2012 07:26 AM |
>complaining that modifying an STL container invalidates all iterators
If you are getting bugs due to that, please rethink how you're doing things. It shouldn't happen. |
|
|
| Report Abuse |
|
|
| |
|
pighead10
|
  |
| Joined: 03 May 2009 |
| Total Posts: 10341 |
|
|
| 12 Aug 2012 10:32 AM |
A better way is to not increment the loop in the top of the loop, so it would be simply
for(iter = b1.begin(); iter != b1.end();{
then have
if((*iter).getAge() > 10) { std::cout << (*iter).getName() << " has died." << std::endl; iter = b1.erase(iter); }else{ iter++; } |
|
|
| Report Abuse |
|
|