我正在寫一個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
的缺省構造函數,該構造函數不存在。然後它會嘗試將調用與我創建的構造函數以及生成的複製構造函數和賦值運算符進行匹配,並根據參數個數對每個構造函數進行失敗。
但是我的代碼中有哪部分調用了默認構造函數?我該如何解決這個錯誤?
已經回答得很好了,但是換一種方式:每一個構造函數首先構造所有子對象* *前的'{}'標記進入體內。如果你不說如何,它會嘗試默認的構造函數。所以你不妨說一下如何使用mem-initializer-list。 – aschepler