这是我上一篇文章的延续。由于它已经关闭,我决定发表一个新帖子。我删除了一半的代码以使其更具可读性。
我读过的一些帖子
Is it possible to use SDL2 with smart pointers?
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结构),我该怎么办?
答案 0 :(得分:6)
std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)>
不是默认可构造的。这意味着
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!";
}
}
当您到达部分***
时,编译器将尝试使用默认构造m_Window
,因为您没有在成员初始化列表中这样做。导致错误的原因是编译器尝试使用默认构造m_Window
。我们可以通过将m_Window = std::move(Create_Window(xWin, yWin));
从构造函数主体中移出并将其放入成员初始化列表中来解决此问题,例如
cGraphics::cGraphics(int xWin, int yWin) : m_Window(Create_Window(xWin, yWin))
{
if (m_Window == nullptr)
{
throw "SDL_Window or SDL_Renderer not ready!";
}
}
如果您不想这样做,则可以委托给默认构造函数,然后像最初一样将其分配给m_Window
。看起来像
cGraphics::cGraphics(int xWin, int yWin) : cGraphics()
{
m_Window = Create_Window(xWin, yWin);
if (m_Window == nullptr)
{
throw "SDL_Window or SDL_Renderer not ready!";
}
}
答案 1 :(得分:4)
这是您定义unique_ptr
的方式:
std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)> m_Window;
这意味着它的构造函数必须使用custome deleter的 instance 来调用,在您的情况下,该函数是SDL_DestroyWindow
-请记住,您告诉指针的是deleter的类型,而不是什么是实际的删除器(在您的情况下为函数指针)。
要使此代码正常工作,必须使用删除器实例正确构造指针,例如:
cGraphics::cGraphics(int xWin, int yWin) :
m_Window{Create_Window(xWin, yWin)} {...}