Tom*_*han 1 c++ constructor rule-of-three
我正在编写一个C++应用程序,其中我有一个Controller带有两个嵌套结构的类,在我的头文件中定义如下:
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...
};
Run Code Online (Sandbox Code Playgroud)
在我的源文件中,我有这个:
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;
};
Run Code Online (Sandbox Code Playgroud)
我觉得很好,但是当我编译时,这就是我得到的(controller.cpp第12行是上面源代码片段的第一行):
g++ -g -Wall -std=c++0x -c -o controller.o controller.cpp
controller.cpp: In constructor ‘palla::Controller::player_command::player_command(void (palla::Controller::* const&)(const args_t&), const palla::Controller::help_message&)’:
controller.cpp:12:93: error: no matching function for call to ‘palla::Controller::help_message::help_message()’
controller.cpp:12:93: note: candidates are:
In file included from controller.cpp:7:0:
controller.hpp:22:3: note: palla::Controller::help_message::help_message(const string&, const string&)
controller.hpp:22:3: note: candidate expects 2 arguments, 0 provided
controller.hpp:19:9: note: palla::Controller::help_message::help_message(const palla::Controller::help_message&)
controller.hpp:19:9: note: candidate expects 1 argument, 0 provided
controller.hpp:19:9: note: palla::Controller::help_message::help_message(palla::Controller::help_message&&)
controller.hpp:19:9: note: candidate expects 1 argument, 0 provided
make: *** [controller.o] Error 1
Run Code Online (Sandbox Code Playgroud)
从我可以推断出,编译器在某处试图调用默认构造函数help_message,它不存在.然后它尝试将调用与我创建的构造函数以及生成的复制构造函数和赋值运算符进行匹配,并且每个参数的数量都失败.
但是我的代码的哪一部分是调用默认构造函数?我该如何解决这个错误?
该player_command()构造函数第一个缺省构造help,然后分配给它:
Controller::player_command::player_command(const Controller::cmd_t& c, const help_message& h) {
cmd = c;
help = h;
};
Run Code Online (Sandbox Code Playgroud)
改为:
Controller::player_command::player_command(const Controller::cmd_t& c, const help_message& h)
: cmd(c),
help(h)
{
};
Run Code Online (Sandbox Code Playgroud)
请参阅初始化列表的好处