[英]Having trouble with pointers in classes
I don't have any trouble when I declare 我声明没有任何麻烦
SDL_Surface *dot = NULL;
globally, but if the SDL_Surface is unique to the class I cant set it to NULL
, so thought it would be fine if I declare it in the constructor like 在全局范围内,但是如果SDL_Surface对于该类是唯一的,则无法将其设置为
NULL
,因此认为如果我在构造函数中声明它像
dot = load_image( "dot.bmp" );
but I still get a 但我仍然得到
Unhandled exception at 0x1002b195 in Uber Mario.exe: 0xC0000005: Access violation reading location 0x0000013c.
on the load_image which returns a SDL_Surface*, sometimes that happened to be because the image was bad or a certain img filetype so i tried another image that does work elsewhere but it still errors like this. 在返回SDL_Surface *的load_image上,有时碰巧是因为该图像不正确或某种img文件类型,所以我尝试了另一幅在其他地方也可以使用的图像,但它仍然会出现这样的错误。
i think im just not using the pointers correctly, even though i studied pointers in school and have read facts on them, for some reason i always have trouble with them. 我认为我只是没有正确使用指针,即使我在学校学习了指针并已阅读了有关指针的事实,出于某种原因,我总是会遇到麻烦。 load_image returns a *SDL_Surface so i need to use a pointer...i think.
load_image返回一个* SDL_Surface,所以我需要使用一个指针...我认为。
here is the class: 这是课程:
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);
}
the load image function: 加载图片功能:
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;
init function 初始化功能
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;
}
By the way, aren't you leaking resources executing that code: 顺便说一句,您是否在泄漏执行该代码的资源:
dot = new SDL_Surface();
dot = load_image( "dot.bmp" );
Doing load_image causes you to loose pointer to SDL_Surface() object hence you cannot delete it later. 进行load_image会使您失去指向SDL_Surface()对象的指针,因此您以后不能删除它。
Answer to your main issue 回答您的主要问题
Call SDL_Init before using SDL_DisplayFormay and it should work. 在使用SDL_DisplayFormay之前先调用SDL_Init,它应该可以工作。 Citation from SDL documentation.
SDL文档中的引文。
Newbie hint
新手提示
You have to call SDL_Init before using the SDL_DisplayFormat function.
您必须在使用SDL_DisplayFormat函数之前调用SDL_Init。 If you don't, your program will crash with an access violation.
否则,您的程序将因访问冲突而崩溃。
You know, dot is a pointer,and it was allocated dynamiclly. 你知道,点是一个指针,它是动态分配的。 SDL_Surface *dot=new SDL_Surface();means that you allocate a storage space in heap,and dot index this space.
SDL_Surface * dot = new SDL_Surface();表示您在堆中分配了一个存储空间,并对该空间进行点索引。 And,the function load_image(string) return a SDL_Surface type value,that is an object,the return object was assignmented to the pointer dot,then pointer dot changed it's direction,then this makes a memory leak.
并且,函数load_image(string)返回一个SDL_Surface类型的值,即一个对象,该返回对象被分配给指针点,然后指针点改变了方向,这将导致内存泄漏。 You can modify the program by this:
您可以通过以下方式修改程序:
SDL_Surface *dot;
dot = load_image("dot.bmp");
thank you. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.