Reputation: 71
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.
Upvotes: 1
Views: 178
Reputation: 17
create object of board and pass parameter in that, then you are good to go I think.
Board board(n);
Upvotes: 1
Reputation: 218323
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};
Upvotes: 4