[英]C++ create an integer variable with the largest integer value supported
[英]Executing C++ function not working when using integer value from variable but works by directly giving integer
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. 我现在在 C++ 中做过几次类似的事情,以前没有遇到过这样的问题。 对不起,如果我没有解释得很好。
所以这是我的代码:
#include <iostream>
#include "Board.h"
using namespace std;
int main()
{
int n;
while (1)
{
n = 5;
Board(n);
//Board(5);
}
return 0;
}
现在,如果我将其执行为“Board(n);”,我已经实施的板 function 拒绝工作; (n 等于 5),但如果我使用“Board(5);”,一切都会按预期工作反而。 我得到的错误是:
main.cpp: In function ‘int main()’:
main.cpp:12:16: error: no matching function for call to ‘Board::Board()’
12 | Board(n);
| ^
我不明白这个错误,因为我没有调用 Board::Board(),我实际上是在调用 Board::Board(int par_n)。
这可能是什么原因造成的? 谢谢。
Board(n)
读作Board n;
-> 定义Board
类型的n
(隐藏先前的int n;
)。 它不会创建临时的。
你可能想要:
Board board(n);
为了保持暂时,你可以做
Board{n};
创建板的 object 并在其中传递参数,那么我认为你对 go 很好。
Board board(n);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.