我正在尝试研究如何将 std::shared_ptr 与自定义删除器一起使用。具体来说,我将它与 SDL_Surface 一起使用:
std::shared_ptr<SDL_Surface>(SDL_LoadBMP(....),SDL_FreeSurface);
编译并运行良好。但是,我想尝试自己的删除器,但不知道该怎么做。SDL_FreeSurface 的文档可在此处找到:
http://sdl.beuc.net/sdl.wiki/SDL_FreeSurface
我在其中发现 SDL_FreeSurface 声明为:
void SDL_FreeSurface(SDL_Surface* surface);
作为测试,并根据该信息,我尝试了以下功能:
void DeleteSurface(SDL_Surface* surface)
{
std::cout << "Deleting surface\n";
SDL_FreeSurface(surface);
}
但是,使用 g++ 编译会出现以下错误:
error: no matching function for call to 'std::shared_ptr<SDL_Surface>::shared_ptr(SDL_Surface*, <unresolved overloaded function type>)'
我已经查看了 gcc std::shared_ptr 实现的 gnu 文档,但无法理解它。我究竟做错了什么?
编辑:我已经缩小了问题的范围,但会留下上面的原始问题。我所拥有的是一个 Game 类,如果我将其剥离为基本实现,则类似于:
class Game {
public:
/* various functions */
private:
void DeleteSurface(SDL_Surface* surface);
bool CacheImages();
std::vector<std::shared_ptr<SDL_Surface> > mCachedImages;
/* various member variables and other functions */
}
执行DeleteSurface
如上,执行CacheImages()
如下:
bool CacheImages()
{
mCachedImages.push_back(std::shared_ptr<SDL_Surface>(SDL_LoadBMP(...),DeleteSurface);
return true;
}
哪个游戏我上面列出的错误。但是,如果我将DeleteSurface()
函数移到Game
类之外而不进行其他更改,则代码会编译。在导致问题的类中包含DeleteSurface
函数是什么意思?Game