没有合适的默认构造函数可用于 std::unique_ptr [英] No appropriate default constructor available for std::unique_ptr
问题描述
这是我上一篇文章的延续.既然已经关了,我决定发个新帖.我删除了一半代码以使其更具可读性.
This is a continuation of my previous post. Since it has already been closed I decided to make a new post. I removed half of the code to make it more readable.
我读过的一些帖子:
关于 SDL_Window 和 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
的默认值.
I understand that this error usually shows up when the compiler cannot locate a default constructor for some of the members. However this is not true as I explicitly declared a default value for the std::unique_ptr
.
如果编译器实际上是在抱怨 SDL_Window
,这是一个不完整的类型(C 结构),我该怎么办?
If the compiler is actually complaining about SDL_Window
, which is an incomplete type (a C struct), what can I do about this?
推荐答案
A std::unique_ptr
不是默认可构造的.这意味着在
A std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)>
is not default constructable. That means in
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
,因为您没有在成员初始化列表中这样做.来自编译器 ro 默认构造 m_Window
的尝试是导致错误的原因.我们可以通过将 m_Window = std::move(Create_Window(xWin, yWin));
移出构造函数体并将其放入成员初始化列表中来解决这个问题,如
When you reach the part ***
the compiler is going to try and default construct m_Window
since you didn't do so in the member initialization list. That attempt from the compiler ro default construct m_Window
is what causes the error. We can fix this by moving m_Window = std::move(Create_Window(xWin, yWin));
out of the constructor body and putting it in the member initialization list like
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
.那看起来像
If you don't want to do that then you can delegate to the default constructor and then assign to m_Window
like you were doing originally. That would look like
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!";
}
}
这篇关于没有合适的默认构造函数可用于 std::unique_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!