正确的线程调用语法?错误:没有匹配对 std::thread::thread(<大括号括起来的初始值设定项列表>)
Proper thread call syntax? error: no matching call to std::thread::thread(<brace-enclosed initializer list>)
>我正在尝试创建一个运行类函数的线程。我认为我做错了什么的地方靠近底部,我有RowChecker r0(puz, 0, s); thread rt0 {r0.check()};
。我的编译器(g++)告诉我有no matching function for call to ‘std::thread::thread(<brace-enclosed initializer list>)’
所以我明白我没有正确拨打电话。格式化此调用以创建新线程的正确方法是什么?
#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;
}
对不起,长度。另外,我知道传递数组是邀请错误加入的好方法。我打算把东西切换到矢量。
你需要将一个函数传递给 thread
的构造函数。在您的代码中,您正在调用r.check()
并将结果传递给 thread
的构造函数,并且接受此类参数的构造函数不存在,因此存在错误。
thread
构造函数采用函数和参数。对于成员函数,第一个参数是this
指针。因此,对于您的代码,您需要:
thread rt0 { &RowChecker::check, &r0 };
那将RowChecker::check
r0
相关文章:
- std::vector的包装器,使数组的结构看起来像结构的数组
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 看起来is_nothrow_constructible_v()在MSVC中被破坏了,我错了吗
- 在std::thread中,joinable()然后join()线程安全吗
- 将大括号括起来的初始值设定项列表作为结构返回
- 学习多线程C++:添加线程不会使执行速度更快,即使它看起来应该
- <Windows>为什么 std::thread::native_handle 返回类型为"long long unsigned int"的值,而不是 void*(又名 HANDLE)?
- 分离一个静态常量 std::thread?
- 尝试使用 std::vector<std::thread时出现静态断言失败错误>
- 使用 thread 类在 C++ 中构造线程的动态数组时出错
- 当指向对象的指针作为参数传递给 std::thread 时,内存可见性
- 如何从 std::thread 返回值
- 在C++中使用并行化的预期速度是多少(不是 OpenMp,而是 <thread>)
- 将 SDL2 与 CMake 链接起来
- 调用重载的"<大括号括起来的初始值设定项列表>"对于对来说就足够了是模棱两可的
- 如何使用connect将qml按钮与同一类的cpp函数连接起来
- 尽管一切看起来都很好,但值不会交换
- 尝试将 c 字符串数组与分隔符连接起来
- 正确的线程调用语法?错误:没有匹配对 std::thread::thread(<大括号括起来的初始值设定项列表>)
- 为什么boost::thread::join看起来这么贵