gpt4 book ai didi

c++ - 在类中使用指针时遇到问题

转载 作者:行者123 更新时间:2023-11-28 07:18:27 25 4
gpt4 key购买 nike

我声明的时候没有任何问题

SDL_Surface *dot = NULL;

全局,但如果 SDL_Surface 对类来说是唯一的,我不能将它设置为 NULL,所以我认为如果我在构造函数中声明它会很好

 dot = load_image( "dot.bmp" );

但我还是得到了一个

Unhandled exception at 0x1002b195 in Uber Mario.exe: 0xC0000005: Access violation reading location 0x0000013c.

在返回 SDL_Surface* 的 load_image 上,有时这恰好是因为图像不好或某个 img 文件类型,所以我尝试了另一个在其他地方可以工作的图像,但它仍然会出现这样的错误。

我认为我只是没有正确使用指针,即使我在学校学习指针并阅读了有关它们的事实,但出于某种原因我总是遇到麻烦。 load_image 返回一个 *SDL_Surface 所以我需要使用一个指针...我想。

这是类:

class Character
{
private:
int yVel, xVel;
int xAcc, yAcc;
int spd, maxV;

int JumpPower;

int FacingRight, FacingLeft;//directing status 0 or 1


bool Flying, onGround;
//Type of particle
SDL_Surface *type;

public:
Shine *myShine;
Animation *walking;
SDL_Surface *dot;

//Offsets
SDL_Rect Rect;

Character();
void handle_input();
void move();
void show();
void togglefly();
void jump();
void whereami();// check and set various characters statuses
};



Character::Character()
{
//Set offsets

Rect.x = 150;
Rect.y = 150;
Rect.w = 20;
Rect.h = 20;

yVel = 0;
xVel = 0;
yAcc = 0;
xAcc = 0;

maxV = 30;
spd = 2;
JumpPower = 40;
Flying = true;

myShine = new Shine(Rect.x, Rect.y);

// walking = new Animation("mario.bmp", 3, 0, 0, Rect.w, Rect.h);

dot = new SDL_Surface();

dot = load_image( "dot.bmp" );

myShine->setpos(Rect);
myShine->setRange(Rect.h*1.5);

}

加载图像函数:

SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;

//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;

//Load the image
loadedImage = IMG_Load( filename.c_str() );

//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage ); //EXCEPTION OCCURES HERE

//Free the old surface
SDL_FreeSurface( loadedImage );

//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}

//Return the optimized surface
return optimizedImage;

初始化函数

    bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}

//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}

//Set the window caption
SDL_WM_SetCaption( "Particle Test", NULL );

//Seed random
srand( SDL_GetTicks() );




//If everything initialized fine
return true;
}

最佳答案

顺便说一句,你不是在泄漏执行该代码的资源吗:

dot = new SDL_Surface();
dot = load_image( "dot.bmp" );

执行 load_image 会导致您丢失指向 SDL_Surface() 对象的指针,因此您以后无法删除它。

回答您的主要问题

在使用 SDL_DisplayFormay 之前调用 SDL_Init,它应该可以工作。引自 SDL 文档。

Newbie hint

You have to call SDL_Init before using the SDL_DisplayFormat function. If you don't, your program will crash with an access violation.

关于c++ - 在类中使用指针时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19886259/

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