Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的unique_ptr认为is有一个空函数指针deleter? 我正试图用C++学习SDL。我创建了一个window.h头文件和一个window.cpp源文件来存储窗口类。在window.h中,它看起来像这样: Class Window { public: Window(); . . . private: std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window; std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer; . . . } 类窗口{ 公众: 窗口(); . . . 私人: std::唯一的ptr窗口; std::唯一的ptr渲染器; . . . }_C++_Sdl_Unique Ptr - Fatal编程技术网

为什么我的unique_ptr认为is有一个空函数指针deleter? 我正试图用C++学习SDL。我创建了一个window.h头文件和一个window.cpp源文件来存储窗口类。在window.h中,它看起来像这样: Class Window { public: Window(); . . . private: std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window; std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer; . . . } 类窗口{ 公众: 窗口(); . . . 私人: std::唯一的ptr窗口; std::唯一的ptr渲染器; . . . }

为什么我的unique_ptr认为is有一个空函数指针deleter? 我正试图用C++学习SDL。我创建了一个window.h头文件和一个window.cpp源文件来存储窗口类。在window.h中,它看起来像这样: Class Window { public: Window(); . . . private: std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window; std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> renderer; . . . } 类窗口{ 公众: 窗口(); . . . 私人: std::唯一的ptr窗口; std::唯一的ptr渲染器; . . . },c++,sdl,unique-ptr,C++,Sdl,Unique Ptr,省略了类中的一些代码。然后,在我的源文件中,在默认构造函数的定义中,我执行以下操作: Window::Window() { window = std::unique_ptr<SDL_Window, void (*)(SDL_Window*)>(nullptr, SDL_DestroyWindow); renderer = std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)>(nullptr, SDL_Dest

省略了类中的一些代码。然后,在我的源文件中,在默认构造函数的定义中,我执行以下操作:

Window::Window() {
  window = std::unique_ptr<SDL_Window, void (*)(SDL_Window*)>(nullptr, SDL_DestroyWindow);
  renderer = std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)>(nullptr, SDL_DestroyRenderer);
}
Window::Window(){
window=std::unique_ptr(nullptr,SDL_-window);
renderer=std::unique_ptr(nullptr,SDL_renderer);
}

然而,当我开始编译时,我被告知,
unique\u ptr[是用null函数指针deleter构建的,据我所知,这是错误的。也许我误解了如何使用
unique\u ptr
的deleter,但我无法找出问题所在。是否有什么我遗漏了或完全误解了我在做什么?

使用成员初始值设定项列表:

Window::Window()
    : windows(nullptr, SDL_DestroyWindow), rendered(nullptr, SDL_DestroyRenderer)
{
    // empty
}

当构造函数的主体运行时,所有成员都应该已经初始化(默认构造,除非您显式执行其他操作,如上文所述)。此后,您只能分配给它们。

问题是,在构造函数中,您对成员
窗口和
渲染器
使用分配而不是初始化。您的成员被隐式默认初始化,这将生成一个错误

但为什么会这样,如何改变呢

如果C++是新的,听起来可能有点奇怪,但是在构造函数函数体被评估之前,任何类成员都被初始化。默认情况下,每个成员都将使用其默认构造函数进行初始化,或者不进行初始化(如果它是像

int
这样的基本类型)。如果您想更改此行为(即,如果您想用不同的东西初始化它,如您所需),则必须使用

例如:

Window::Window() :   // <-- put a colon here
    windows(nullptr, SDL_DestroyWindow),     // here comes the member init list
    rendered(nullptr, SDL_DestroyRenderer)
{
}

Window::Window()://谢谢,这很有道理!我想我已经被Java宠坏了,这可能是一个你从未听过的短语。使用初始值设定项列表后,一切正常。你介意解释一下为什么所有成员都在构造函数之前初始化吗?它们必须在某个地方初始化。C++的设计是,只要运行构造函数的主体,就已经完成了这个步骤。无法更好地解释,抱歉:)但如果您感兴趣,我在上面发布的wiki链接可以更好地解释。