I’m a beginner with C++ and now have a problem with one of my classes. I have a vector list of my Sprite class and I want to get one of items in the list and give it to a method parameter of another class but it just tell me the Subscript range is out of vector. I examined the list to see is it really containing any item and it was right, the list working great.
My list:
vector<Core::Graphic::cSprite> Sprites;
My method:
Core::Logic::cGameObject::cGameObject(std::string Name, Core::Graphic::cSprite* Sprite, float X, float Y, int Depth)
{
// Set fields
this->Name = Name;
this->Sprite = *Sprite;
// Add to active sprites
for(int i = 0; i < this->Sprite.Images.size(); i++)
{
// Create temporaroy sprite
sf::Sprite tempSprite;
tempSprite.SetImage(this->Sprite.Images[i]);
this->ActiveSprite.push_back(tempSprite);
}
this->X = X;
this->Y = Y;
this->Depth = Depth;
this->ImageIndex = 0;
this->ImageNumber = this->Sprite.SubFrames;
}
My sprite constructor:
Core::Graphic::cSprite::cSprite(std::string Name, vector<std::string> ImagesFileNames)
{
// Check input
if(Name != "" && ImagesFileNames.max_size() > 0)
{
this->Name = Name;
for(int i = 0; i < ImagesFileNames.size(); i++)
{
sf::Image tempImage;
if(tempImage.LoadFromFile(ImagesFileNames[i])){
this->Images.push_back(tempImage);
}
}
this->SubFrames = this->Images.max_size();
}
}
I have a cGameObjectManaher class to manage game objects and it has a method like below:
Game.GameObjectManager.AddGameObject("obj_intro_1", &Game.SpriteManager.Sprites[0], 0, 0, 0);
I checked everything in my codes but nothing exceeds out of the lists. I think it’s the problem of the code that I implemented.
Thanks.
Your code is using
max_size()which returns the system maximum of items you could theoretically add to the vector instead ofsize()which returns the actual size of the vector. The code you’re showing is probably not the code crashing, but instead some code depending on this->Subframes that is set to a HUGE number in your latter code snippet.As an example, on my machine;
returns
4611686018427387903instead of0which you’re probably expecting.