【问题标题】:I have an error, and i am unsure why it is not working (threads)我有一个错误,我不确定它为什么不工作(线程)
【发布时间】: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


【解决方案1】:
thread tme((timer()));

应该改成下面的样子,假设你想把 int 100 传递给定时器函数

thread tme(timer, 100);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    • 2020-11-25
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多