1

I seem to be having a issue where I have a function that is not working when giving an integer through a variable but the function works perfectly by directly giving integer when executing function. I've done a similar thing in C++ a few times now and have had no such issue before. Sorry if I did not explain too well.

So this is my code:

#include <iostream>
#include "Board.h"
using namespace std;

int main()
{
    int n;
    while (1)
    {
        n = 5;
        Board(n);
        //Board(5);
    }
    return 0;
}

Now the Board function I have implemented refuses to work if I execute it as "Board(n);" (n equalling 5), but everything works as intended if I use "Board(5);" instead. The error I get is:

main.cpp: In function ‘int main()’:
main.cpp:12:16: error: no matching function for call to ‘Board::Board()’
   12 |         Board(n);
      |                ^

I do not understand this error as I'm not calling to Board::Board(), I'm actually calling to Board::Board(int par_n).

What could be causing this? Thanks.

Sebastian
  • 71
  • 5
  • As mentioned by others it would be great if you show the code for the ```board``` function as well. – iSythnic Apr 22 '21 at 08:07
  • [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/8746648) – asynts Apr 22 '21 at 08:41

2 Answers2

4

Board(n) is read as Board n; -> definition of n of type Board (which hides previous int n;). it doesn't create a temporary.

You probably want:

Board board(n);

To keep temporary, you might do

Board{n};
Jarod42
  • 203,559
  • 14
  • 181
  • 302
1

create object of board and pass parameter in that, then you are good to go I think.

Board board(n);