缺少默认构造函数-但I';我不叫它? 我写了一个C++应用程序,其中有一个包含两个嵌套结构的控制器类,在我的头文件中定义如下: class Controller { struct help_message { // controller.hpp, line 19 std::string summary; std::string details; help_message(const std::string&, const std::string&); }; struct player_command { cmd_t cmd; help_message help; // cmd_t is my own typedef, irrelevant for this question player_command(const cmd_t&, const help_message&); }; // more members... };

缺少默认构造函数-但I';我不叫它? 我写了一个C++应用程序,其中有一个包含两个嵌套结构的控制器类,在我的头文件中定义如下: class Controller { struct help_message { // controller.hpp, line 19 std::string summary; std::string details; help_message(const std::string&, const std::string&); }; struct player_command { cmd_t cmd; help_message help; // cmd_t is my own typedef, irrelevant for this question player_command(const cmd_t&, const help_message&); }; // more members... };,c++,constructor,rule-of-three,C++,Constructor,Rule Of Three,在我的源文件中,我有以下内容: Controller::player_command::player_command(const Controller::cmd_t& c, const help_message& h) { cmd = c; help = h; }; Controller::help_message::help_message(const std::string& s, const std::string& d) { su

在我的源文件中,我有以下内容:

Controller::player_command::player_command(const Controller::cmd_t& c, const help_message& h) {
    cmd = c;
    help = h;
};

Controller::help_message::help_message(const std::string& s, const std::string& d) {
    summary = s;
    details = d;
};
我认为这很好,但当我编译时,我得到的是(controller.cpp第12行是上面源代码片段的第一行):

根据我的推断,编译器正在某处尝试调用
help\u message
的默认构造函数,该构造函数不存在。然后,它尝试将调用与我创建的构造函数以及生成的复制构造函数和赋值运算符匹配,并且在参数数量上都失败

但是我的代码的哪一部分调用了默认构造函数?如何修复此错误?

使用
player\u命令()
构造函数首先默认构造
help
,然后分配给它:

Controller::player_command::player_command(const Controller::cmd_t& c, const help_message& h) {
    cmd = c;
    help = h;
};
改为:

Controller::player_command::player_command(const Controller::cmd_t& c, const help_message& h)
:  cmd(c),
   help(h)
{
};
请参见播放器命令()构造函数首先默认构造
帮助
,然后分配给它:

Controller::player_command::player_command(const Controller::cmd_t& c, const help_message& h) {
    cmd = c;
    help = h;
};
改为:

Controller::player_command::player_command(const Controller::cmd_t& c, const help_message& h)
:  cmd(c),
   help(h)
{
};

请参见

您没有使用避免复制构造函数的语法。参数是通过引用传递的(无复制构造函数),但当分配给ivar时,它确实是被复制的

Class::Class(const Variable &var) {
  this->var = var; // assignment here, this invokes copy contructor!
}
应使用以下语法:

Class::Class(const Variable &var) : var(var) { }

您没有使用避免复制构造函数的语法。参数是通过引用传递的(无复制构造函数),但当分配给ivar时,它确实是被复制的

Class::Class(const Variable &var) {
  this->var = var; // assignment here, this invokes copy contructor!
}
应使用以下语法:

Class::Class(const Variable &var) : var(var) { }

player\u命令
struct包含一条
help\u消息
(帮助),并且
help\u消息
没有默认构造函数。调用
player\u命令
constructor时,默认情况下,帮助成员变量将被默认构造。您将立即为给定参数分配帮助,但这将在默认构造参数之后进行。相反,将构造函数更改为类似以下内容:

Controller::player_command::player_command(const Controller::cmd_t& c, const help_message& h) : cmd(c), help(h) 
{}

这将调用cmd和help成员变量的复制构造函数,而不是进行默认构造和赋值。

player\u命令
结构包含一条
help\u消息
(帮助),并且
help\u消息
没有默认构造函数。调用
player\u命令
constructor时,默认情况下,帮助成员变量将被默认构造。您将立即为给定参数分配帮助,但这将在默认构造参数之后进行。相反,将构造函数更改为类似以下内容:

Controller::player_command::player_command(const Controller::cmd_t& c, const help_message& h) : cmd(c), help(h) 
{}

这将调用cmd和help成员变量的复制构造函数,而不是执行默认构造和赋值。

微妙的错误(至少对我来说)-快速捕获和良好的解释。荣誉一点也不讨厌。初始化和赋值之间只有一个(不那么微妙的)区别。微妙的错误(至少对我来说)-快速捕捉和很好的解释。荣誉一点也不讨厌。初始化和赋值之间只有一个(不那么微妙的)区别。答案已经很好了,但换一种说法:每个构造函数在进入主体之前先构造所有子对象,然后在
{}
标记中。如果您不说如何,它将尝试默认构造函数。因此,您不妨说如何使用mem-initializer-list.com已经回答得很好了,但换言之:每个构造函数首先构造所有子对象,然后在
{}
标记中进入主体。如果您不说如何,它将尝试默认构造函数。因此,您不妨说明如何使用mem初始值设定项列表。