简体   繁体   English

正确的线程调用语法? 错误:没有匹配调用std :: thread :: thread( <brace-enclosed initializer list> )

[英]Proper thread call syntax? error: no matching call to std::thread::thread(<brace-enclosed initializer list>)

I'm trying to create a thread which runs a class function. 我正在尝试创建一个运行类函数的线程。 The place where I think I'm doing something wrong is near the bottom in main where I've got RowChecker r0(puz, 0, s); thread rt0 {r0.check()}; 我认为我做错了什么的地方是在Main底部靠近RowChecker r0(puz, 0, s); thread rt0 {r0.check()}; RowChecker r0(puz, 0, s); thread rt0 {r0.check()}; . My compiler (g++) tells me that there's no matching function for call to 'std::thread::thread(<brace-enclosed initializer list>)' So I understand that I'm not making the call correctly. 我的编译器(g ++)告诉我, no matching function for call to 'std::thread::thread(<brace-enclosed initializer list>)'因此我知道我没有正确进行调用。 What is the proper way to format this call to make a new thread? 格式化此调用以创建新线程的正确方法是什么?

#include <iostream>
#include <thread>

using namespace std;

class Sum
{
private:
    int sum;
public:
    Sum();
    int getSum();
    void addSum();
};

class RowChecker
{
private:
    int puz[9][9];
    int myRow;
    Sum* sum;
public:
    RowChecker(int puzzel[9][9], int row, Sum* shared);
    void check();
};

Sum::Sum() {
    sum = 0;
}

int Sum::getSum() {
    return sum;
}

void Sum::addSum() {
    ++sum;
}

RowChecker::RowChecker(int puzzel[9][9], int row, Sum* shared) {
    for (int i=0; i<9; ++i) {
        for (int j=0; j<9; ++j) {
            puz[i][j] = puzzel[i][j];
        }
    }
    myRow = row;
    sum = shared;
}

void RowChecker::check() {
    char table[] = {0,0,0, 0,0,0, 0,0,0};
    for (int i=0; i<9; ++i) {
        if (puz[i][myRow]<10 && puz[i][myRow]>=0) {
            table[puz[i][myRow]] = 1;
        }
    }
    for (int i=0; i<9; ++i) {
        if (table[i]==0) {
            return;
        }
    }
    sum->addSum();
}

void readPuzzel(int puz[9][9]){
    for (int i=0; i<9; ++i) {
        for (int j=0; j<9; ++j) {
            puz[i][j] = rand() % 10;
        }
    }
}

int main()
{
    Sum s;

    int puz[9][9];
    readPuzzel(puz);

    RowChecker r0(puz, 0, &s);

    thread rt0 {r0.check()};

    rt0.join();

    cout << "Sum s is " << s.getSum() << endl;

    return 0;
}

Sorry for the length. 对不起,长度。 Also, I am aware that passing arrays around is a great way to invite bugs in. I'm planning to switch things over to vectors. 另外,我知道传递数组是邀请bug的好方法。我打算将其切换为向量。

You need to pass a function to the constructor of thread . 您需要将一个函数传递给thread的构造函数。 In your code, you are calling r.check() and passing the result to the constructor of thread , and a constructor which takes such an argument does not exist, hence the error. 在您的代码中,您正在调用 r.check()并将结果传递给thread的构造函数,并且采用此类参数的构造函数不存在,因此会出现错误。

The thread constructor takes a function and arguments. thread构造函数接受一个函数和参数。 And in the case of a member function, the first argument is the this pointer. 对于成员函数,第一个参数是this指针。 So for your code you need: 因此,对于您的代码,您需要:

thread rt0 { &RowChecker::check, &r0 };

That will call RowChecker::check on r0 . 这将在r0上调用RowChecker::check

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:没有匹配的 function 调用 'std::vector<pet*> ::向量(<brace-enclosed initializer list> )'</brace-enclosed></pet*> - error: no matching function for call to 'std::vector<Pet*>::vector(<brace-enclosed initializer list>)' 使用std :: min时发生错误“没有匹配函数调用&#39;min( <brace-enclosed initializer list> )“” - Error when using std::min “no matching function for call to ‘min(<brace-enclosed initializer list>)’” 没有匹配的 function 调用 'std::vector::push_back(<brace-enclosed initializer list> )'</brace-enclosed> - no matching function for call to ‘std::vector::push_back(<brace-enclosed initializer list>)’ C++ 中的可调用 class 对象:没有匹配的 function 用于调用 'std::tuple<t> ::元组(<brace-enclosed initializer list> )'</brace-enclosed></t> - Callable class objects in C++ : no matching function for call to ‘std::tuple<T>::tuple(<brace-enclosed initializer list>)’ 调用重载 &#39;<brace-enclosed initializer list> &#39; with suffss for pair 是不明确的 - call of overloaded ‘<brace-enclosed initializer list>’ with suffices for pairs is ambiguous 错误:没有匹配的 function 调用 'uWS::TemplatedApp<false> ::ws<main()::userdata> (常量字符 [3],<brace-enclosed initializer list> )'</brace-enclosed></main()::userdata></false> - error: no matching function for call to 'uWS::TemplatedApp<false>::ws<main()::UserData>(const char [3], <brace-enclosed initializer list>)' 呼叫过载 <brace-enclosed initializer list> 是暧昧的,如何处理? - call of overloaded <brace-enclosed initializer list> is ambiguous, how to deal with that? 用大括号括起来的初始化程序列表 - brace-enclosed initializer list 错误:无法转换&#39; <brace-enclosed initializer list> ()&#39;从&#39; <brace-enclosed initializer list> &#39;到&#39;结构&#39; - Error: could not convert '<brace-enclosed initializer list>()' from '<brace-enclosed initializer list>' to 'struct' 无法从大括号括起来的初始化列表转换为std元组 - Could not convert from brace-enclosed initializer list to std tuple
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM