Can't push_back a class into an object vector inside a for loop

user3906336

I cannot call a function that does a push_back into a vector

void GameState::InitialiseBullet(float x, float y, float vx, float vy)
{
    Bullet* bullets = new Bullet();
    bullets->SetSize(5.f, 20.f);
    bullets->AddFrame("./images/bullet.png");
    bullets->Play();
    bullets->SetX(x);
    bullets->SetY(y);
    bullets->velocityX = vx;
    bullets->velocityY = vy;

    bullets->isActive = true;
    gameObjects.push_back(bullets);
}

when it is inside the following for loop

for (auto& object : gameObjects)
{
    //Determine the type at runtime
    if (dynamic_cast<Player*>(object) != 0)
    {
        //Process player-specific logic
        PlayerLogic(dynamic_cast<Player*>(object), a_fTimeStep);
    }

//Determine the type at runtime
if (dynamic_cast<Bullet*>(object) != 0)
{
    //Process bullet-specific logic
    BulletLogic(dynamic_cast<Bullet*>(object), a_fTimeStep);
}
if (dynamic_cast<Enemy*>(object) != 0)
{
    //Process enemy-specific logic
    Enemy* enemy = dynamic_cast<Enemy*>(object);
    EnemyLogic(enemy, lowerAliens);
    if (enemy->GetIsActive() == true)
    {
        allDead = false;
    }
}

//Update and draw our objects
object->Update(a_fTimeStep);
object->Draw();
}

The piece of code that calls the function:

if (createBullet == true)
{
    InitialiseBullet(bulletX, bulletY, 0, 500);
    createBullet = false;
}

That code works when outside the for loop. However, I need the for loop to provide access to each of my player, enemy and bullet objects. Is there a way to push_back to a vector inside a for loop that is based on the same vector? I get a "Expression: Vector iterators incompatible" error when it's inside the loop. Any ideas? New to C++ programming.

Andy Newman

It's just a vector of pointers, so it's not very big.

The objects being added is probably even smaller.

You could make a copy of the vector and iterate over the copy while inserting into the real one.

You could put new items into a new, empty vector while you iterate, and then splice them onto the real one at the end.

To delete objects, you could do either of those things, or you could simply set a flag "isZombie" and then remove all the zombies at the end.

These aren't the only answers, but they all work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't push_back a class containing an ofstream pointer to a vector

From Dev

Why can't I push_back to a vector of const elements?

From Dev

Can't push_back an unique_ptr in a vector

From Dev

Which C++ vector push_back is called for objects created inside a loop

From Dev

string Vector push_back failing in class

From Dev

Implementing push_back(T&& c) in custom Vector<T> class

From Java

std::vector::push_back() doesn't compile on MSVC for an object with deleted move constructor

From Dev

push_back/append or appending a vector with a loop in C++ Armadillo

From Dev

Push_back on a 2D vector through for loop

From Dev

Is there a way to push_back multiple variables into a vector in a for loop?

From Dev

Using scanf and while loop with vector push_back

From Dev

Vector iterator incompatible after push_back() of a new object

From Dev

Populating values of a struct inside a class using push_back

From Dev

Populating values of a struct inside a class using push_back

From Dev

vector push_back doesn't work c++

From Dev

Pointer to object in a class: push_back and pointers conflicts

From Dev

push_back/emplace_back a shallow copy of an object into another vector

From Dev

vector push_back in STL?

From Dev

Vector and push_back() behavior

From Dev

Unable to push_back to vector

From Dev

Push array into object inside loop

From Dev

making boost::ptr_vector container class push_back function

From Dev

Cant push_back() non copyable object to vector reference C++

From Dev

std::string in object being deleted during push_back to std::vector

From Dev

Can C++ std::vector handle push_back from multithreads at the same time?

From Dev

vector::push_back(myVec) on to heap. Can you avoid myVec header information on stack?

From Dev

can i check content of values that have been push_back to a vector?

From Dev

Can C++ std::vector handle push_back from multithreads at the same time?

From Dev

C++ STD Vector push_back doesn't seem to work

Related Related

  1. 1

    Can't push_back a class containing an ofstream pointer to a vector

  2. 2

    Why can't I push_back to a vector of const elements?

  3. 3

    Can't push_back an unique_ptr in a vector

  4. 4

    Which C++ vector push_back is called for objects created inside a loop

  5. 5

    string Vector push_back failing in class

  6. 6

    Implementing push_back(T&& c) in custom Vector<T> class

  7. 7

    std::vector::push_back() doesn't compile on MSVC for an object with deleted move constructor

  8. 8

    push_back/append or appending a vector with a loop in C++ Armadillo

  9. 9

    Push_back on a 2D vector through for loop

  10. 10

    Is there a way to push_back multiple variables into a vector in a for loop?

  11. 11

    Using scanf and while loop with vector push_back

  12. 12

    Vector iterator incompatible after push_back() of a new object

  13. 13

    Populating values of a struct inside a class using push_back

  14. 14

    Populating values of a struct inside a class using push_back

  15. 15

    vector push_back doesn't work c++

  16. 16

    Pointer to object in a class: push_back and pointers conflicts

  17. 17

    push_back/emplace_back a shallow copy of an object into another vector

  18. 18

    vector push_back in STL?

  19. 19

    Vector and push_back() behavior

  20. 20

    Unable to push_back to vector

  21. 21

    Push array into object inside loop

  22. 22

    making boost::ptr_vector container class push_back function

  23. 23

    Cant push_back() non copyable object to vector reference C++

  24. 24

    std::string in object being deleted during push_back to std::vector

  25. 25

    Can C++ std::vector handle push_back from multithreads at the same time?

  26. 26

    vector::push_back(myVec) on to heap. Can you avoid myVec header information on stack?

  27. 27

    can i check content of values that have been push_back to a vector?

  28. 28

    Can C++ std::vector handle push_back from multithreads at the same time?

  29. 29

    C++ STD Vector push_back doesn't seem to work

HotTag

Archive