Search code examples
c++vectorpush-back

C++ vector.push_back adds objects twice when adding an object once


I am creating a DirectX program which needs to be able to dynamically add objects into a list (or vector) for use the render function. I need to be able to access these objects through the "[]" operator which is the reason i am using vectors instead of lists. The problem is that when I add an object using push_back, it stores that object twice. For example; If the vector is empty and I'm adding the first object, it will store that object into index 0 and into index 1. this creates problems for my render function which must loop through every object and render them. This results in each object being rendered twice.

Code which defines the vector(SpriteList2) located in header file:

class Level1 : public Dx11DemoBase// Define Level1 Class
{
public:
    vector<Sprite1> SpriteList2;//Define Vector
};

Code to add the objects located in cpp file:

bool Level1::LoadContent()//funtion in the Level1 class
{
    Sprite1 Sprite_1({0.0f, 0.0f, 3.0f});//Define Sprite Object
    SpriteList2.push_back(Sprite_1);//Add Sprite Object to Vector

    Sprite1 Sprite_2({ 100.0f, 00.0f, 2.0f });//Define Sprite Object
    SpriteList2.push_back(Sprite_2);//Add Sprite Object to Vector

    Sprite1 Sprite_3({ 100.0f, -50.0f, 1.0f });//Define Sprite Object
    SpriteList2.push_back(Sprite_3);//Add Sprite Object to Vector
    return true;
}

Code to loop through the objects located in cpp file:

void Level1::Render()//funtion in the Level1 class
{
    for (Sprite1 sprite : SpriteList2)//Loop through Vector
    {
        //Code to use the "sprite" object in rendering functions
        worldMat = sprite.GetWorldMatrix();//Retrieve values from each sprite object
    }
}

note The code which adds the objects (shown above) is located in a separate cpp file and the vector is defined in a header file.

I have tried with different types (ex:int) with the same result. This problem does not occur if the vector is defined outside of the header file but the vector has to be defined inside the header file so that the render function can access it.


Solution

  • There is nothing in the code shown that would make objects be inserted twice, so it comes down to some old-fashioned debugging skills. For a start, change the loader function to be something like:

    bool Level1::LoadContent() {
        std::cout << "Enter LoadContent with size " << SpriteList2.size() << '\n';
    
        Sprite1 Sprite_1({0.0f, 0.0f, 3.0f});//Define Sprite Object
        SpriteList2.push_back(Sprite_1);//Add Sprite Object to Vector
    
        Sprite1 Sprite_2({ 100.0f, 00.0f, 2.0f });//Define Sprite Object
        SpriteList2.push_back(Sprite_2);//Add Sprite Object to Vector
    
        Sprite1 Sprite_3({ 100.0f, -50.0f, 1.0f });//Define Sprite Object
        SpriteList2.push_back(Sprite_3);//Add Sprite Object to Vector
    
        std::cout << "Exit  LoadContent with size " << SpriteList2.size() << '\n';
    
        return true;
    }
    

    (using a different debug method than cout if you need to). This will hopefully make it obvious what order things are happening in. It's very unlikely that you'll see:

    Enter LoadContent with size 0
    Exit  LoadContent with size 6
    

    Far more likely that you'll see some other combination which will show something external to that code is causing the problem, such as calling the function more than once, or affecting the vector externally to the function.