views:

144

answers:

2

my works like this i shot airplanes and if the shot toughed an airplane it change there positions(airplane and the shot) than it will delete them. if i toughed an airplane the airplane position gets changed. than it gets deleted than my health reduces. well that works well expect that it breaks when i have about 2-4 airplanes left and i toughed one of them and shot one of them my game breaks it pops a box says vector out of range... i have had something like this problem before i fixed it because i new where was the problem but the box doesn't give me any information where it is the problem. here my full code

Thanks in Advance

note: i don't think i can go any lowwer with my code i have put new one

+2  A: 

Learn to 'refactor': for example, code like line 241 looks like it would be better as a subroutine.

Anyway, to find where this particular problem is, do "Debug/Break All" with the debugger while the error box is being displayed; and then look at the debugger's "Call stack" window, to see what code is being executed which triggers the popping of the box.

ChrisW
I deleted my answer because you beat me to it by 4 seconds. A shame, because it was the first time I had an opportunity to use the `<kbd>`-tag. Also, +1 for trying to help in spite of the terrible question.
Space_C0wb0y
ok the hint you gave me to look in call stack helped me a bit. i found where is the error coming. now i know what is really happaning. when i touch an airplane it deletes part of the vector so like if the vector size is 3 it will be 2 than, at that time my game will check if vector has toughed a shot yet, so i when i touch the vector will delete a part of vector, that part is being check if it has been got hit by a shot so that will cause my error because when it is checking it can't find the vector
Ramiz Toma
+3  A: 

Well I just had a quick look at that monstrosity, and I'm not going to look too far into it but right off the bat I see a problem.

I don't know if this is related to this particular problem you are asking about, but this type of thing isn't going to work the way you think:

for(long index=0; index < (long)RegularShots.vshots.size(); ++index) 
{ 
   RegularShots.vshots[index].y-=2;
   // Delete Shots
   if(RegularShots.vshots[index].y<-16)
   {
     RegularShots.vshots.erase(RegularShots.vshots.begin()+index);
   }
}

When you erase an item from the vector, the next item in the vector is moved down one, so every time you erase an item, you are skipping over the next item and not performing your test on it.

A quick fix here would be to do an index-- after the erase call.

But based on what I've seen, I would seriously recommend that you do some reading on STL containers and iterators in particular. And then read some books on good coding style ;)

Gerald
i did wat you said but it didn't help in my case but i got wat you meant and thanks for pointing it out ;)
Ramiz Toma