私はC++の初心者です。今では自分のクラスに問題があります。 Sprite
クラスのベクトルリストがあり、リスト内の項目の1つを取得して別のクラスのメソッドパラメータに渡したいのですが、それはちょうどSubscript range is out of vector
です。私はリストが実際にどのアイテムを含んでいるかを調べました。ユーザー定義のクラスのインスタンスを取得する
マイリスト:
vector<Core::Graphic::cSprite> Sprites;
私の方法:
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;
}
私のスプライトのコンストラクタ:
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();
}
}
私はゲームオブジェクトを管理するためのcGameObjectManaher
クラスを持っており、それが持っています以下のようにします。
Game.GameObjectManager.AddGameObject("obj_intro_1", &Game.SpriteManager.Sprites[0], 0, 0, 0);
私のコードですべてを確認しましたが、リストから外れるものはありません。私が実装したコードの問題だと思います。
ありがとうございました。
'Game.SpriteManager.Sprites'を使用する前に初期化していますか? –
私は 'Game.GameObjectManager.AddGameObject'を使っている行とまったく同じです。私は私のゲームオブジェクトクラスコンストラクタの全身を何が間違っているかを見てコメントしましたが、それはただエラーを与え続けます。 – MahanGM
はい、私はここに書きませんでした。すべてが正しいですが、このリストは本当の問題です! – MahanGM