[英]C++ vector issues
無論出於什么原因,我的應用程序都會崩潰。 我是媒介的新手,所以我有更多的愚蠢之處。
#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 );
}
我到底在做什么錯? 我推了兩個值(完成測試后將是x和y ints),但是當它到達offset.x = c[0];
時似乎崩潰了offset.x = c[0];
腳本的一部分。
我認為肯定c [0]和c [1]應該都等於10。有什么建議嗎?
沒錯,[0]和[1]都應為10。
只要確保您首先真正調用過newProjectile(),並確保沒有其他問題,例如無效的目標指針等等。
該代碼似乎還可以,但是調用的順序很重要。 確保在調用drawCurrentState()之前先調用newProjectile(),例如:
Projectile * projectile = new Projectile();
projectile->newProjectile(1,1);
projectile->drawCurrentState( ... );
也許向量c在讀取c [0]的行是空的。 檢查c.size()的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.