I just recently started experimenting with vectors and I keep running into this problem when erasing a spot in the vector. Once it executes the erase() function it causes the application to crash and a memory access violation occurs.
Here is the code for the function:
bool moveBullets(void)
{
int i = 0;
for(std::vector<sf::Sprite>::iterator it = laserList.begin(); it != laserList.end(); it++)
{
laserList[i].move(0.0f, -2.0f);
int id = 0;
for(std::vector<sf::Sprite>::iterator s = enemy.begin(); s != enemy.end(); s++)
{
if(checkCollision(laserList[i], enemy[id]))
{
laserList.erase(laserList.begin() + i);
//enemy.erase(s);
std::cout << "COLLISION";
}
id++;
}
i++;
}
return 0;
}
Basically what this function does it run through a vector and checks another vector for a collision. So vector A will check for a collision all the way through vector B. Once it has fully checked vector B it will go to the next position in vector A and repeat.
However it works fine until it runs the erase() function. Then it causes a memory access error.
Thanks for the help! :-)