【发布时间】:2020-01-19 01:48:52
【问题描述】:
我正在尝试创建一个线程并让它在函数中运行。由于某种原因,它不起作用。这是我的代码。我不知道出了什么问题以及错误的含义。它给我的错误如下:没有构造函数“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);
}
}
}
【问题讨论】:
-
timer接受一个参数。你希望线程用什么参数来调用它?
标签: c++ multithreading