1

I'm getting the above error in C++ when trying to initialise an object of type Board. The constructor for board takes in two integers, so it's

Board::Board(int w, int h)

And I'm trying to create a Connect Four game. The ConnectFour.h file has the following:

Board b;

in its private variables, and the constructor in ConnectFour.cpp is this:

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

among other things, obviously.

It's giving me the error:

In constructor ‘ConnectFour::ConnectFour(int, int)’:|

error: no matching function for call to ‘Board::Board()’|

note: candidates are:|

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

note: candidate expects 2 arguments, 0 provided|

If anyone could lend a hand, I'd really appreciate it.

Edit: Turns out I was being a bit silly. Thanks guys.

  • 1
    See http://stackoverflow.com/questions/2308646/different-ways-of-constructing-an-object-in-c – jarmod Apr 23 '13 at 23:26

2 Answers2

5

You need to supply a constructor for board that takes no parameters to work with your Board b; code, or you need to pass the width, height to the object when it's created Board b(width, height); or you can place the initialization of board into initialization list of ConnectFour


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

so that it has the info needed when ConnectFour is created.

Finally, you could keep a pointer to the Board object in the parent class and dynamically create it in the parent class's constructor. This requires more care and probably use of smart pointers to handle the creation and destruction of the object properly.

Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
1

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);
    }
};
Petzey
  • 11
  • 1
  • 3
  • Not quite solving OP's problem. He has Board as a member of ConnectFour, meaning he has to do a bit more C++ gymnastics than you are showing here. – Michael Dorgan Apr 23 '13 at 23:47