std::unique_ptr 没有合适的默认构造函数可用

No appropriate default constructor available for std::unique_ptr

这是我之前 post 的延续。由于它已经关闭,我决定制作一个新的 post。我删除了一半代码以使其更具可读性。

我阅读的一些 post:

Smart pointers with SDL

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 结构),我该怎么办?

A 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!";
    }
}

这是您定义 unique_ptr 的方式:

std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)> m_Window;

这意味着,必须使用客户删除器的 实例 调用它的构造函数,在您的情况下,函数 SDL_DestroyWindow - 记住,您告诉指针什么是删除器的类型,而不是实际删除器的类型(在您的情况下是函数指针)。

要使此代码正常工作,您必须使用删除器实例正确构造指针,例如:

cGraphics::cGraphics(int xWin, int yWin) : 
                     m_Window{Create_Window(xWin, yWin)} {...}