gpt4 book ai didi

C++ vector 问题

转载 作者:太空宇宙 更新时间:2023-11-04 15:51:33 24 4
gpt4 key购买 nike

无论出于何种原因,我的应用程序总是崩溃。我是 vector 的新手,所以我很可能有些愚蠢。

#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 整数)但是当它到达脚本的 offset.x = c[0]; 部分时它似乎崩溃了。

我认为 c[0] 和 c[1] 都应该等于 10。有什么建议吗?

最佳答案

你是对的,[0] 和 [1] 都应该是 10。

只要确保你真的先调用了newProjectile(),并确保没有其他问题,例如无效的目标指针等。

关于C++ vector 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7428376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com