我有一个Block
调用SDL_FreeSurface(surface)
析构函数的类。在main()
我创建 Block 的实例时,该对象正常运行,但是当我在另一个Control
具有vector<Block> block_vector
作为数据成员的类中使用它时,当我将实例添加Block
到block_vector
. 我缩小了Block
调用时的析构函数的问题SDL_FreeSurface(surface)
。向向量中添加对象与 is 有什么关系吗?问题是什么?
class Block{
public:
Block(int x, int y);
~Block();
void Load_Image(MediaFunctions &M_Functions);
void SetPosition(int x, int y);
void BlitBlock(SDL_Event &event, MediaFunctions &M_Functions, SDL_Surface *destination);
bool DetectionNames(SDL_Event &event, MediaFunctions &M_Functions, SDL_Surface *destination);
bool DetectionHours(SDL_Event &event, MediaFunctions &M_Functions, SDL_Surface *destination);
bool return_error();
private:
SDL_Surface *block_surface_names;
SDL_Surface *block_surface_hours;
SDL_Surface *block_names_detected;
SDL_Surface *block_hours_detected;
SDL_Rect block_rect_names;
SDL_Rect block_rect_hours;
bool error;
};
//the problem
Block::~Block(){
SDL_FreeSurface(block_surface_hours);
SDL_FreeSurface(block_surface_names);
SDL_FreeSurface(block_hours_detected);
SDL_FreeSurface(block_names_detected);
}
//when doing this
void Control::HandleEvents(SDL_Event &event, MediaFunctions &M_Functions){
if(event.type == SDL_KEYDOWN){
if( event.key.keysym.sym == SDLK_a ){
//append a block instance
BlockVector.push_back(Block (Block(100,100)));
BlockVector.at(block_index).Load_Image(M_Functions);
block_index++;
}
}
}