这是我上一篇文章的延续。由于它已经关闭,我决定发表新帖子。我删除了一半的代码以使其更具可读性。
我读过的一些帖子:
使用 SDL 的智能指针 https://stackoverflow.com/questions/24251747/smart-pointers-with-sdl
是否可以将 SDL2 与智能指针一起使用? https://stackoverflow.com/questions/48672399/is-it-possible-to-use-sdl2-with-smart-pointers
关于 SDL_Window 和 unique_ptr 的几个问题 https://stackoverflow.com/questions/29424877/couple-of-questions-about-sdl-window-and-unique-ptr
class cGraphics
{
public:
// Creator functions
std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)> Create_Window(int xWin, int yWin);
// ctor & dtor
cGraphics() : m_Window(nullptr, SDL_DestroyWindow) {}
cGraphics(int xWin, int yWin);
~cGraphics();
private:
std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)> m_Window;
};
cGraphics::cGraphics(int xWin, int yWin)
{
m_Window = std::move(Create_Window(xWin, yWin));
if (m_Window == nullptr)
{
throw "SDL_Window or SDL_Renderer not ready!";
}
}
cGraphics::~cGraphics()
{
IMG_Quit();
SDL_Quit();
}
std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)> cGraphics::Create_Window(int xWin, int yWin)
{
return std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)>(SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, xWin, yWin, SDL_WINDOW_SHOWN), SDL_DestroyWindow);
}
编译器抱怨说:
'std::unique_ptr<SDL_Window,void (__cdecl *)(SDL_Window *)>::unique_ptr': no appropriate default constructor available
据我所知,当编译器无法找到某些成员的默认构造函数时,通常会出现此错误。然而这不是真的,因为我明确声明了默认值std::unique_ptr
.
如果编译器实际上在抱怨SDL_Window
,这是一个不完整的类型(C 结构体),我该怎么办?