C++ 数组分配

C++ 数组分配,c++,sdl,C++,Sdl,我有一个名为SpriteCollection的类,在这个类中我用SDL加载图像。此类具有一个名为: SDL_Surface* sprites[]; 我认为这是对的,尽管我不确定。在同一个类中,我有一个方法: void addNewSprite(SDL_Surface* sprite){ this->sprites[n_sprites+1] = new SDL_Surface; this->sprites[n_sprites+1] = IMG_Load("sprit

我有一个名为SpriteCollection的类,在这个类中我用SDL加载图像。此类具有一个名为:

SDL_Surface* sprites[];
我认为这是对的,尽管我不确定。在同一个类中,我有一个方法:

void addNewSprite(SDL_Surface* sprite){

    this->sprites[n_sprites+1] = new SDL_Surface;
    this->sprites[n_sprites+1] = IMG_Load("spritepath.jpg");
    this->n_sprites++;
}
另一个用于检索SDL_曲面以在屏幕上绘制:

SDL_Surface getSprite(int sprite_index){

    return this->sprites[sprite_index];
}
要在我使用的屏幕上绘制:

Draw(x_position, y_position, this->sprite->getSprite[0], screen);
我正常加载图像;一切正常,但是IDE返回了一个关于指针和SDL_Surface*和SDL_Surface*之间的转换的错误

我做错了什么

编辑:错误消息:

E:\cGame.cpp|71|error: cannot convert `SDL_Surface' to `SDL_Surface*' for argument `1' to `int SDL_UpperBlit(SDL_Surface*, SDL_Rect*, SDL_Surface*, SDL_Rect*)'|
应该是:

SDL_Surface* getSprite(int sprite_index)
应该是:

SDL_Surface* getSprite(int sprite_index)
在返回类型为SDL_曲面的getSprite函数中,您试图返回SDL_曲面*。也许你的意思是:

SDL_Surface* getSprite(int sprite_index){
  return this->sprites[sprite_index];
}
此外,这些线路非常可疑:

this->sprites[n_sprites+1] = new SDL_Surface;
this->sprites[n_sprites+1] = IMG_Load("spritepath.jpg");
首先,动态分配一个新的SDL_曲面并存储指向它的指针。然后,通过将IMG_Load调用的结果分配给该指针,您将摆脱该指针。现在,您将永远无法删除曲面,因为您丢失了指向它的指针。您可能会考虑封装您的sprite,并使用习惯用法来处理SDL_曲面的分配

除此之外,最好使用std::vector而不是数组。

在返回类型为SDL_Surface的getSprite函数中,尝试返回SDL_Surface*。也许你的意思是:

SDL_Surface* getSprite(int sprite_index){
  return this->sprites[sprite_index];
}
此外,这些线路非常可疑:

this->sprites[n_sprites+1] = new SDL_Surface;
this->sprites[n_sprites+1] = IMG_Load("spritepath.jpg");
首先,动态分配一个新的SDL_曲面并存储指向它的指针。然后,通过将IMG_Load调用的结果分配给该指针,您将摆脱该指针。现在,您将永远无法删除曲面,因为您丢失了指向它的指针。您可能会考虑封装您的sprite,并使用习惯用法来处理SDL_曲面的分配

除此之外,最好使用std::vector而不是数组。

请发布您收到的确切错误消息。请发布您收到的确切错误消息。