[英]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.