Why does this function get wrong argument from iterator? - c++

I'm writing a Space Invaders clone for IT project on uni. All the methods work, but I have a problem with deleting enemies.
I redone my code to use iterators. I store enemies in vector(horizontal) of vectors(vertical) of Enemy. The code works fine until i shoot more than 2 projectiles at a time when it gives me an error.
if (projectiles.size() != 0)
{
for (auto itr_columns = enemies.begin(); itr_columns != enemies.end(); itr_columns++)
{
for (auto itr_rows = itr_columns->begin(); itr_rows != itr_columns->end();)
{
if (projectiles.size() == 0)
{
break;
}
for (auto itr_projectiles = projectiles.begin(); itr_projectiles != projectiles.end();)
{
if (itr_projectiles->Collision(*itr_rows))
{
itr_projectiles = projectiles.erase(itr_projectiles);
itr_rows = itr_columns->erase(itr_rows);
}
else
{
itr_rows++;
itr_projectiles++;
}
}
}
}
}
That's the error i get:
line: if (itr_projectiles->Collision(*itr_rows))
Expression: can't dereference out of range vector iterator

Look at the instruction :
itr_rows = itr_columns->erase(itr_rows);
By doing that, you invalidate the iterators of itr_columns while you're are in the loop, which does use these iterators.
Never call functions invalidating iterators when you a are in a loop.

Related

list.remove_if() crashing the program

I'm working on a game and I'm trying to add collectables. I'm trying to remove the object from the list after the player has collided with it, but it ends up crashing and says:
Unhandled exception thrown: read access violation.
__that was 0xDDDDDDE9.
It says this on the for loop statement, but I think it has to do with the remove_if() function.
Here is my code:
for (sf::RectangleShape rect : world1.level1.brainFrag) {
collides = milo.sprite.getGlobalBounds().intersects(rect.getGlobalBounds());
if (collides == true) {
world1.level1.brainFrag.remove_if([rect](const sf::RectangleShape val) {
if (rect.getPosition() == val.getPosition()) {
return true;
}
else {
return false ;
}
});
brainFrag -= 1;
collides = false;
}
}
if (brainFrag == 0) {
milo.x = oldPos.x;
milo.y = oldPos.y;
brainFrag = -1;
}
I don't understand your approach, you loop the rects, then when you find the one you want to remove, you search for it again through list<T>::remove_if.
I think that you forgot about the fact that you can use iterators in addition to a range-based loop:
for (auto it = brainFrag.begin(); it != brainFrag.end(); /* do nothing */)
{
bool collides = ...;
if (collides)
it = world1.level1.brainFrag.erase(it);
else
++it;
}
This allows you to remove the elements while iterating the collection because erase will take care of returning a valid iterator to the element next to the one you removed.
Or even better you could move everything up directly:
brainFrag.remove_if([&milo] (const auto& rect) {
return milo.sprite.getGlobalBounds().intersects(rect.getGlobalBounds())
}
A side note: there's no need to use an if statement to return a boolean condition, so you don't need
if (a.getPosition() == b.getPosition()
return true;
else
return false;
You can simply
return a.getPosition() == b.getPosition();

Remove what's in one list from the other list

I have two lists which contain a bunch of elements of the same type:
std::list<Part> allParts = step.getSubParts();
std::list<Part> toRemove;
for (Part part : allParts)
{
for (Part partTwo : allParts) {
if (part.getEdges() == partTwo.getEdges())
if (part.getFaces() == partTwo.getFaces())
if (part.getShells() == partTwo.getShells())
if (part.getVertices() == partTwo.getVertices())
if (part.getWires() == partTwo.getWires())
{
part.addAmount(1);
toRemove.push_back(partTwo);
}
}
}
I have tried iterating through both and remove from the one but I'm constantly getting the list iterators are incompatible error. This is my latest attempt:
std::list<Part>::iterator it;
for (it = step.getSubParts().begin(); it != step.getSubParts().end();)
{
std::list<Part>::iterator i;
for (i = toRemove.begin(); i != toRemove.end();)
{
if (it->getEdges() == i->getEdges())
if (it->getFaces() == i->getFaces())
if (it->getShells() == i->getShells())
if (it->getVertices() == i->getVertices())
if (it->getWires() == i->getWires())
{
it = step.getSubParts().erase(it);
}
else
{
it++;
}
i++;
}
}
Everything I have tried doesn't work. What is the correct way to do this?
You should consider remove_if or erase_if rather than doing your own erase with the hazard of making iterator invalid within a loop.
By the way, you should write predicate like:
if (it->getEdges() == i->getEdges() &&
it->getFaces() == i->getFaces() &&
it->getShells() == i->getShells() &&
it->getVertices() == i->getVertices() &&
it->getWires() == i->getWires()) {
// do something
}
Your code makes people difficult to understand your purpose(at least me).
erase and erase_if
First of all, it would be a good idea to follow the Don't Repeat Yourself principle and write a comparison function for future use:
auto compare_parts = [](const Part& p1, const Part& p2) -> bool {
return ( (p1.getEdges() == p2.getEdges())
and (p1.getFaces() == p2.getFaces())
and (p1.getShells() == p2.getShells())
and (p1.getVertices() == p2.getVertices())
and (p1.getWires() == p2.getWires()) );
}
You would rewrite the first cycle using it and see how much more simple it looks.
Then why not use c++ built-in methods to erase the elements from the list using the function we wrote? This uses new feature in c++ called binding parameters that would aid us here
#include <functional>
using namespace std::placeholders;
for (auto&& badPart : toRemove) {
auto isBad = std::bind(compare_parts, badPart, _1);
step.getSubParts().remove_if(isBad);
}
And that's how you remove special entries from the list.
I think the cleanest way would be:
1. Implement equality operator for the class Part
You can either put it inside or outside the class, it would look like this if you implement it as an external function
inline bool operator==(const Part& lhs, const Part& rhs) {
return lhs.getEdges() == rhs.getEdges() &&
lhs.getFaces() == rhs.getFaces() &&
lhs.getShells() == rhs.getShells() &&
lhs.getVertices() == rhs.getVertices() &&
lhs.getWires() == rhs.getWires();
}
2. Implement the loop, I would recommend using iterators
This is just one way of doing it
if (allParts.size() > 1) {
for(auto partIt = std::begin(allParts); partIt != std::end(allParts); partIt++) {
for(auto partIt2 = std::next(partIt); partIt2 != std::end(allParts);) { // Manual increasing because we erase stuff
if(*partIt == *partIt2) { // Previously implemented equility operator
partIt->AddAmount(1);
partIt2 = allParts.erase(partIt2); // If erase, use the returned iterator as your next `Part`
} else {
partIt2++; // Only increment if nothing was erased (when you erase iterators get invalidated)
}
}
}
}

I can get objects from a list and copy them, but how do I delete the objects I copied?

I'm trying to find all the processes in the blockedProcess list with the specified event number, copy them into a transferList, and then remove them. Copying and moving the Process objects works fine, but I can't figure out how to remove those Process objects from blockedProcess afterwards.
ProcessQueue findEventFlag(int eventnum)
{
ProcessQueue transferProcess;
Process process;
list<Process>::iterator it;
for (it = blockedProcess.begin(); it != blockedProcess.end(); it++)
{
process = *it;
if (process.getEvent() == eventnum)
{
process.setState("READY");
process.setEvent(-1);
transferProcess.enqueue(process);
}
}
return transferProcess;
}
Building on the comments, try this (which relies on the fact that std::list::erase conveniently returns an iterator to the next item in the list):
ProcessQueue findEventFlag(int eventnum)
{
ProcessQueue transferProcess;
Process process;
list<Process>::iterator it = blockedProcess.begin();
while (it != blockedProcess.end())
{
process = *it;
if (process.getEvent() == eventnum)
{
process.setState("READY");
process.setEvent(-1);
transferProcess.enqueue(process);
it = process.Erase (it);
}
else
it++;
}
return transferProcess;
}
You can also transfer an element from one list to another with std::list::splice which would avoid copying your process object and might therefore be more efficient, something like:
ProcessQueue findEventFlag(int eventnum)
{
ProcessQueue transferProcess;
Process process;
list<Process>::iterator it = blockedProcess.begin();
while (it != blockedProcess.end())
{
process = *it;
list<Process>::iterator next = std::next (it);
if (process.getEvent() == eventnum)
{
process.setState("READY");
process.setEvent(-1);
transferProcess.splice(transferProcess.begin (), blockedProcess, it);
}
it = next;
}
return transferProcess;
}

Unable to delete a list element in C++

I am working on a small game and came across a big problem with lists.
Here's my code:
void cCollisionManager::checkCollision(cPlayer * pPlayer, std::list<cAsteroid*> *asteroidList, std::list<cShot*> *ShotList)
{
sf::FloatRect PlayerBox = pPlayer->getSprite()->getGlobalBounds();
for (auto it : *asteroidList) {
for (auto es : *ShotList) {
sf::FloatRect asteroidboundingBox = it->getSprite()->getGlobalBounds();
sf::FloatRect ShotBox = es->getSprite().getGlobalBounds();
if (asteroidboundingBox.intersects(ShotBox)) {
it = asteroidList->erase(it);
*pPlayer->pPunkte += 1;
std::cout << *pPlayer->pPunkte << std::endl;
}
if (asteroidboundingBox.intersects(PlayerBox)) {
if (*pPlayer->phealth >= 0.f)
*pPlayer->phealth -= 0.5f;
}
}
}
}
I used SFML and basically everything works. But if I want to delete the colliding asteroid and the shot, the programs exits with an error. In the if loop I tried to erase the object, but the compiler also gives an error saying that the argument type is not the same as the object type I am giving to it.
EDIT
I had another look at the other question, you recommended to me, but still I haven't found out how to solve that problem. So if I changed my code to a while loop, the game couldn't handle it, because the Collision Manager is actually called in every single Call of the SFML main loop. So it would just get stuck in my collision loop. So I changed my code a bit, but still, things are not working.
Don't modify sequences that are being enumerated with range-for. Use
iterators and the appropriate result of an erase. – WhozCraig
This is actually the answer to it. I did the mistake - using a for loop and not a while loop and so I had some big issues and bad construction ideas for my code - luckily everything now works!
Here is my final code:
auto it = asteroidList->begin();
auto es = ShotList->begin();
while (it != asteroidList->end()) {
sf::FloatRect PlayerBox = pPlayer->getSprite()->getGlobalBounds();
sf::FloatRect asteroidboundingBox = (*it)->getSprite()->getGlobalBounds();
while (es != ShotList->end())
{
sf::FloatRect ShotBox = (*es)->getSprite().getGlobalBounds();
if (asteroidboundingBox.intersects(ShotBox)) {
it = asteroidList->erase(it);
es = ShotList->erase(es);
std::cout << "Asteroid destroyed" << std::endl;
*pPlayer->pPunkte += 1;
std::cout << *pPlayer->pPunkte << std::endl;
}
if (es != ShotList->end())
es++;
}
if (asteroidboundingBox.intersects(PlayerBox))
{
if (*pPlayer->phealth > 3.f) {
*pPlayer->phealth -= 5.f;
it = asteroidList->erase(it);
}
else
*pPlayer->pBStateAlive = false;
}
if (it != asteroidList->end()) {
it++;
es = ShotList->begin();
}
}
}

Qt application freezes when accessing QList<QLlnkedList<QUrl>* > from NetworkAccessManager->get() callback

My application freezes when I am trying to access a QList<QLinkedList<QUrl>* > within the replyFinished(QNetworkReply* networkReply); slot of a QNetworkAccessManager.
I basically loop once over the list add something to the list and then exit the function:
foreach (QLinkedList<QUrl>* list, mList) {
if (list->front() == url) {
list->prepend(someUrl);
mNetworkAccessManager->get(someUrl);
return;
}
}
In the other scenario I do the following:
QList<QLinkedList<QUrl>* >::iterator it = mList.begin();
while (it != mList.end()) {
QLinkedList<QUrl>* list = *it;
if (list->front() == networkReply->url()) {
// some operation with list->back();
mList.erase(it);
delete list;
break;
}
}
Is there any way to make this construct safe?
I was caught in an endless loop, since
QList<QLinkedList<QUrl>* >::iterator it = mList.begin();
while (it != mList.end()) {
QLinkedList<QUrl>* list = *it;
never increased the iterator ..