私のアプリケーションは何らかの理由でクラッシュし続けます。私はベクタにとって新しいので、私のところには何か愚かなものがあります。C++のベクタの問題
#include <iostream>
#include <vector>
using namespace std;
class Projectile
{
private:
SDL_Surface *projectile;
void load();
public:
int count;
vector<int> c;
vector< vector<int> > p;
int positionX;
int positionY;
Projectile();
void newProjectile(int, int);
void drawCurrentState(SDL_Surface*);
};
...
...
void Projectile::newProjectile(int x, int y)
{
positionX = x;
positionY = y;
c.push_back(10);
c.push_back(10);
//p.push_back(c); //trying to start off simple before i do multidimensional.
}
void Projectile::drawCurrentState(SDL_Surface* destination)
{
SDL_Rect offset;
offset.x = c[0]; //will eventually me the multidimensional p vector
offset.y = c[1]; //
SDL_BlitSurface(projectile, NULL, destination, &offset);
}
私はここで間違っていますか?私は2つの値を戻しますが(テストを終えた後xとyのintになります)、スクリプトのoffset.x = c[0];
部分に到達するとクラッシュするようです。
確かに、c [0]とc [1]の両方が10になるはずです。
「newProjectile」の前に 'drawCurrentState'を呼び出しましたか? 'offset.x'への代入の前に' std :: cout << c.size()<< std :: endl;を挿入してください。 –
このコードからはわかりません。私の推測では 'Projectile'の別のインスタンスで作業しています(コピーコンストラクタが表示されていない場合は不完全です)。コピー後に空の' c'ベクターになります...ロングショット – sehe
おそらく 'vector :: at '' vector :: operator [] 'の代わりにヒントを与えます。サブスクリプトが有効でない場合、out_of_range例外がスローされます –