缺少默认构造函数-但我没有调用它吗? [英] Default constructor missing - but I'm not calling it?

查看:137
本文介绍了缺少默认构造函数-但我没有调用它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个C ++应用程序,其中有一个 Controller 类,该类带有两个嵌套结构,在头文件中定义如下:

I'm writing a C++ application in which I have a Controller class with two nested structs, defined in my header file as follows:

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...
};

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

In my source file, I have this:

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

which I thought was fine, but when I compile, this is what I get (controller.cpp line 12 is the first line of the source snippet above):

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 的默认构造函数,该构造函数不存在。然后,它将尝试将调用与我创建的构造函数以及生成的copy-constructor和赋值运算符进行匹配,并使每个参数的失败数目。

From what I can deduce, the compiler is somewhere trying to call the default constructor of help_message, which doesn't exist. It then tries to match the call up with the constructor I created as well as with the generated copy-constructor and assignment operator, and failing each on the number of arguments.

但是我的代码的哪一部分正在调用默认构造函数?以及如何解决此错误?

But what part of my code is calling a default constructor? And how do I fix this error?

推荐答案

player_command()构造函数首先默认构造 help ,然后分配给它:

The player_command() constructor first default constructs help and then assigns to it:

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)
{
};

请参见初始化列表的优点

这篇关于缺少默认构造函数-但我没有调用它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆