缺少默认构造函数 - 但我不调用它?

Default constructor missing - but I'm not calling it?

本文关键字:调用 默认 构造函数      更新时间:2023-10-16

我正在编写一个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...
};

在我的源文件中,我有这个:

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行是上面源代码片段的第一行(:

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

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

但是,我的代码的哪一部分在调用默认构造函数呢?我该如何修复这个错误?

player_command()构造函数首先默认构造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)
{
};

请参阅初始化的好处列表

player_command结构包含一个help_message(帮助(,并且help_message没有默认构造函数。当调用player_command构造函数时,默认情况下将默认构造帮助成员变量。您将立即为给定参数指定帮助,但这将在默认构造之后进行。相反,将构造函数更改为类似以下内容:

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

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

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

Class::Class(const Variable &var) {
  this->var = var; // assignment here, this invokes copy contructor!
}

您应该使用以下语法:

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