Tristus
Tristus

Reputation: 143

using .size() member function on a vector passed by reference

I'm having some confusion with the usage of the .size() member function of vector.

So what I have is an object that displays a series of bitmaps in sequence, these bitmaps are stored as pointers in a vector. This vector is then passed by reference "&" in the construction of my "animation" object, which does all the work cycling through the bitmaps.

Everything works as I expected except that calling .size() on my referenced vector of bitmap pointers, which does not return anything, though I know the vector has contents.

This then causes the animation to cycle through normally, then it trips up because its trying to access an element that is out of bounds, due to .size() returning nothing, messing up my bounds checking.

My only guess is that my syntax is not correct, or that i'm not properly understanding the usage.

#include "animation.h"
#include "helperFunctions.h"
#include <vector>
#include <iostream>

animation::animation(int x, int y, SDL_Renderer* screenRenderer, std::vector<SDL_Texture*>& sprites) {
    _x = x;
    _y = y;
    _sprites = &sprites;
    _frames =  sprites.size() - 1;///this is to be used as an indexer, eg. 0 frames is 1 in reality, 3 is 4...
    _currentFrame = 0;///first frame index
    mainScreen = screenRenderer;
    _finished = false;
}

animation::animation(const animation& orig) {

}

animation::~animation() {
    std::cout << "animation object deleted!" << std::endl;
}

void animation::cycleFrame() {
    applyTexture(_x, _y, (*_sprites)[_currentFrame], mainScreen);
    std::cout << "_currentFrame: " << _currentFrame << std::endl;
    std::cout << "_frames      : " << _frames << std::endl;
    _currentFrame++;
    if (_currentFrame == _frames) {
        _finished = true;
    }
}

Also I should add that i'm not asking for help with anything SDL, just the whole vector.size() thing.

Thank in advance, any help would be greatly appreciated.

UPDATE:

So I did some digging and it seems like .size() is also returning as 0 on the vector before it is even being passed to the constructor... in main() I have:

std::vector<SDL_Texture*> explosion16;        
explosion16.reserve(16);
for (int i = 0; i < 16; i++) {
    explosion16[i] = loadTexture(loadImage("gfx/explosions/explosion_16/e" + to_string(i) + ".bmp"));
}
cout << "explosion16.size() : " << explosion16.size() << endl;/////this returns as zero, 0

Upvotes: 1

Views: 1520

Answers (3)

MKR Harsha
MKR Harsha

Reputation: 115

 if(sprites.size() > 0)
 _frames =  sprites.size() - 1;

Upvotes: 0

Loonquawl
Loonquawl

Reputation: 1076

Based on your comment: If you use reserve(), that does not increment the size, only the capacity. You should either use resize(), and than you can use the indexer to initialize the members, or keep reserve and use push_back() instead of [].

Upvotes: 4

mystic_coder
mystic_coder

Reputation: 472

in below line :

_frames =  sprites.size() - 1;

_frames might be -1 in case sprites.size() is zero.

Upvotes: 0

Related Questions