简体   繁体   English

没有匹配的函数调用C ++

[英]No matching function call C++

I'm getting the above error in C++ when trying to initialise an object of type Board. 尝试初始化Board类型的对象时,在C ++中出现上述错误。 The constructor for board takes in two integers, so it's board的构造函数采用两个整数,因此

Board::Board(int w, int h)

And I'm trying to create a Connect Four game. 我正在尝试创建“四连环”游戏。 The ConnectFour.h file has the following: ConnectFour.h文件具有以下内容:

Board b;

in its private variables, and the constructor in ConnectFour.cpp is this: 在其私有变量中,而ConnectFour.cpp中的构造函数是这样的:

ConnectFour::ConnectFour()
{
   Board b(7, 6);

among other things, obviously. 除其他外,显然。

It's giving me the error: 这给了我错误:

In constructor 'ConnectFour::ConnectFour(int, int)':| 在构造函数“ ConnectFour :: ConnectFour(int,int)”中:|

error: no matching function for call to 'Board::Board()'| 错误:没有匹配的函数可以调用'Board :: Board()'|

note: candidates are:| 注意:候选人是:|

note: Board::Board(int, int)| 注意:Board :: Board(int,int)|

note: candidate expects 2 arguments, 0 provided| 注意:考生需要2个参数,提供0个|

If anyone could lend a hand, I'd really appreciate it. 如果有人可以伸出援手,我将非常感激。

Edit: Turns out I was being a bit silly. 编辑:原来我有点傻。 Thanks guys. 多谢你们。

You need to supply a constructor for board that takes no parameters to work with your Board b; 您需要为电路板提供一个不带任何参数的构造函数来与您的电路板一起工作Board b; code, or you need to pass the width, height to the object when it's created Board b(width, height); 代码,或者在创建对象时需要将宽度,高度传递给对象Board b(width, height); or you can place the initialization of board into initialization list of ConnectFour 或者您可以将板子的初始化放入ConnectFour的初始化列表中


ConnectFour::ConnectFour() :
b(7,6)
{
}

so that it has the info needed when ConnectFour is created. 以便在创建ConnectFour时具有所需的信息。

Finally, you could keep a pointer to the Board object in the parent class and dynamically create it in the parent class's constructor. 最后,您可以在父类中保留一个指向Board对象的指针,并在父类的构造函数中动态创建它。 This requires more care and probably use of smart pointers to handle the creation and destruction of the object properly. 这需要更多的注意,并且可能使用智能指针来正确处理对象的创建和破坏。

I'm guessing something like this 我猜是这样的

class board
{
public:
    board::board(int w,int h)
    {
        w = 0;
        h = 0;
    }
};

class connectFour
{
    connectFour::connectFour(int, int)
    {
        board b(7,9);
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM