“无匹配函数调用”在构造函数中 [英] "No matching function call" in constructor

查看:228
本文介绍了“无匹配函数调用”在构造函数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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)

然后它列出了Board的构造函数。

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.

我用g ++编译。

推荐答案


错误:没有匹配的函数调用'Board :: Board()'

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

表示 Board 缺少deafault构造函数。在 Solver 的构造函数中,您可能正在执行以下操作:

means that class Board is missing the deafault constructor. In the constructor of Solver you are probably doing something like:

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


b $ b

所以你必须定义默认的构造函数或调用具有一些参数的适当的构造函数。

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).

这篇关于“无匹配函数调用”在构造函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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