C++ 构造函数中的“没有匹配的函数调用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19524100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:56:07  来源:igfitidea点击:

"No matching function call" in constructor

c++functionobjectconstructormatching

提问by Jonathan Wrona

This is the constructor declaration that I have in my "solver.h" file.

这是我在“solver.h”文件中的构造函数声明。

Solver(const Board &board_c, int max_moves_c);

When trying to compile I get the following error...

尝试编译时出现以下错误...

solver.cpp: In constructor 'Solver::Solver(const Board&, int)':
solver.cpp:6:55: error: no matching function for call to 'Board::Board()'
  Solver::Solver(const Board &board_c, int max_moves_c)

And then it lists the candidates which are the Board constructors.

然后它列出了作为董事会构造者的候选人。

I'm not sure what I'm doing wrong as I see no reason why I should be getting this error.

我不确定我做错了什么,因为我看不出为什么我应该收到这个错误。

I am compiling with g++.

我正在用 g++ 编译。

回答by LihO

error: no matching function for call to 'Board::Board()'

错误:没有用于调用“Board::Board()”的匹配函数

means that class Boardis missing the deafault constructor. In the constructor of Solveryou are probably doing something like:

意味着该类Board缺少默认构造函数。在Solver您的构造函数中,您可能正在执行以下操作:

Solver::Solver(const Board &board_c, int max_moves_c) {
    Board b; // <--- can not construct b because constructor is missing
    ...
}

so you either have to define the default constructor or invoke the appropriate constructor with some arguments.

所以你要么定义默认构造函数,要么用一些参数调用适当的构造函数。

"And then it lists the candidates which are the Board constructors."

“然后它列出了董事会构建者的候选人。”

That's because compiler wants to help you so it lists possible constructors that are actually available (defined).

那是因为编译器想要帮助你,所以它列出了实际可用(定义)的可能构造函数。