Access Attribute When Iterating Through a List

Hi, I'm a beginner to C++ and am having some trouble with my list. I first define a struct named "bullet' like so:

struct bullet {
bool alive;
bullet()
alive = true;
};

Then, I create a list of objects of type "bullet": list<bullet> bullet_list; In my program, I frequently add new bullets to the list and delete them. At first, I was using a vector, but then I read that using a list was better for my uses because of how often I create/delete bullets from various positions in the list (I actually did this so frequently that I would get buffer overrun errors, which is why I'm now trying lists).

Anyways, when I was using a vector, I could use the following code to check if the bullet was still "alive":

But now that I am using a list, I cannot. I get the following error: Error 1 error C2676: binary '[' : 'std::list<_Ty>' does not define this operator or a conversion to a type acceptable to the predefined operator What do I have to change in order to make this work?

With bullet_list.erase(bullet_list.begin()+iter); is fixed when just making it iter, since by the looks of it you were attempting to delete the element where the iterator is currently placed. Anyway depends on rest of your code but with commenting out 1 or 2 lines which weren't defined with the rest of the code available, I got that to compile without error once the changes were made, of course don't know if it works as you'd expect, since being 3AM my brain is a little fried now too! :)

Well, good news and bad news! As for the good news, the code that you provided gets rid of compiler errors! However, for the bad news, I'm still getting a bad error in-game.

Since the error is related to this code, I'll just keep it going in this thread. So, let me give you some background information about my game. It is a FPS-type game, so in the simplest manner, you have a gun and you shoot at enemies by clicking the mouse. During the game loop, I run the above function to check whether or not each bullet is still in play. If they are, each bullet is moved forward by a certain velocity; if not, they are removed from the game.

I posted about this on Dark GDK's (game library I use) forum, and they said it was unrelated to the library and that it was just a C++ error. So, that brings me here! Why am I getting the error? I'm almost positive it has to do with the manipulation of my bullet_list, as if I don't remove anything from the list, I don't get the error. I think that sometimes when I shoot new bullets, the program is not quite done deleting the old bullets from the list, so it ends up writing over the old list, causing it to write over itself in memory. Is this correct?

If it helps, when I receive the error, Visual C++ brings up line 800 of the "list" library: _Nextnode(_Prevnode(_Pnode)) = _Nextnode(_Pnode); Does anybody know how I can fix this error?

The problem is in line 12. Executing bullet_list.erase(iter); causes the element to which iter refers to be deleted from the list. Doing sinvalidates iter. On the next trip through the while loop, iter is invalid, so ++iter has undefined effect.

One way to deal with this problem is to increment iter only when it is known to be valid. The following rewrite is slightly sneaky, but I think it will work:

The point is that bullet_list.erase(iter++); copies iter , increments it (which causes it to refer to a value that will survive the call to erase , and then erases the element to which iter formerly referred. This erasure is perfectly safe, and ensures that the value of iter is appropriate for the next time through the loop.

EDIT: Perfect! It works! Thank you so much! So I can hopefully avoid this in the future, could you explain to me what the difference between "++iter" and "iter++" is? They seem to be performing different functions and I would love to know how you came up with this brilliant piece of code. I can see how you are incrementing iter when the bullet is alive, but why do you pass iter++ to bullet_list.erase ? Why doesn't it erase the next bullet in the list instead of the current one?

The problem is in line 12. Executing bullet_list.erase(iter); causes the element to which iter refers to be deleted from the list. Doing sinvalidates iter. On the next trip through the while loop, iter is invalid, so ++iter has undefined effect.

One way to deal with this problem is to increment iter only when it is known to be valid. The following rewrite is slightly sneaky, but I think it will work:

The point is that bullet_list.erase(iter++); copies iter , increments it (which causes it to refer to a value that will survive the call to erase , and then erases the element to which iter formerly referred. This erasure is perfectly safe, and ensures that the value of iter is appropriate for the next time through the loop.

Oops, my bad... I had a feeling something were slightly up but being so tired it didn't twig. Thanks for the correction.

EDIT: Perfect! It works! Thank you so much! So I can hopefully avoid this in the future, could you explain to me what the difference between "++iter" and "iter++" is? They seem to be performing different functions and I would love to know how you came up with this brilliant piece of code. I can see how you are incrementing iter when the bullet is alive, but why do you pass iter++ to bullet_list.erase ? Why doesn't it erase the next bullet in the list instead of the current one?

Thanks everybody for all your help.

++iter & iter++ refer to both pre-increment & post-increment, pre-increment increases the value before the expression is evaluated, post-increment as you guessed increases the value after the expression but he does say why it doesn't erase the next bullet in the list, it copies iter, increments it then erases the element it formerly referred too.

Here is what people usually do when creating a bullet class. Because bullets get
created and destroyed so fast, and because there are so many of them or can be. You
are better of using some raw arrays instead. Put a maximum cap on the number of bullets created. For example you can do something like this :

If you execute bullet_list.erase(iter) , that invalidates iter . So before executing bullet_list.erase(iter) , it is necessary to compute the new value to be placed into iter , because there will be no opportunity to do so later.

Thank you everybody for all your help. All of your explanations really helped me; I understand it pretty well. And thanks for the suggestion firstPerson, I might use that in the future, but currently that is above my level and I don't like to use others' code especially when I have no idea how it works.

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...

Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...