c++ - 我有一个错误,我不确定它为什么不工作(线程)
问题描述
我正在尝试创建一个线程并让它在一个函数中运行。由于某种原因,它不起作用。这是我的代码。我不知道出了什么问题以及错误的含义。它给我的错误如下:没有构造函数“std::thread::thread”的实例与参数列表匹配。另一个错误是:“std::thread::thread”:3 个重载都不能转换所有参数类型。
void f1opp(int hit) {
thread tme((timer()));
getline(cin, choi);
if (choi == "punch")
{
cout << "You try to punch him" << endl;
ran = rand() % 3;
if (ran == 1)
{
cout << "You hit him for 3 damage" << endl;
i = 1 + 123456789;
}
else
{
cout << "You miss" << endl;
Sleep(1000);
}
}
tme.join();
}
// the timer program
void timer(int hit)
{
if (hit >= 3)
{
for (i = 1; i <= 10000; i++)
{
if (s == 0)
{
cout << "They were able to hit you" << endl;
hit = hit + 1;
break;
}
system("cls");
s--;
cout << "He attacks you in:" << s << endl;
Sleep(1000);
}
}
}
解决方案
thread tme((timer()));
应该改为下面,假设你想将 int 100 传递给定时器函数
thread tme(timer, 100);
推荐阅读
- c++ - 如何在 ios 应用程序上运行 dlib 的简单对象检测器
- git - Git Credentials Libsecret:调用 StartServiceByName 时出错,已达到超时
- flutter - 在 Dart 中将子类分配给工厂构造函数
- c# - 如何从 Azure 函数中获取多个 blob?
- flutter - android studio无法识别flutter项目
- flutter - 如何在颤振构建窗口中包含 dll
- oracle - 在存储过程中的 PL/SQL 或 SQL Oracle Developer 中引发异常
- python - 如何将 python 中的多线程编程模型转换为 async/await 模型?
- python - 获取pandas df中所有时期的最大值和最大值出现的时间
- rust - 如何转移自定义令牌