首页 > 解决方案 > 我有一个错误,我不确定它为什么不工作(线程)

问题描述

我正在尝试创建一个线程并让它在一个函数中运行。由于某种原因,它不起作用。这是我的代码。我不知道出了什么问题以及错误的含义。它给我的错误如下:没有构造函数“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);



        }
    }
}

标签: c++multithreading

解决方案


thread tme((timer()));

应该改为下面,假设你想将 int 100 传递给定时器函数

thread tme(timer, 100);

推荐阅读