【发布时间】:2016-11-26 06:42:02
【问题描述】:
我正在尝试编译以下示例,但出现错误。下面是代码:
class A {
private:
//variables
public:
A(int a,int b){
//assign variables
}
void C(){
// do something
}
int D(){
// do something
}
void E(){
}
};
int main(){
A* temp = new A(a,b);
temp->C;
std::thread t;
t(&A::D,A);
t.join();
temp->E;
return 0;
}
当我使用pthread 和std=c++11 标志编译上述代码时,出现以下错误。以下是错误信息:
expected primary-expression before ‘)’ token
t(&A::D,A);
【问题讨论】:
-
没有
#include列表。在main()中,没有a。没有b。temp->C;毫无意义。同样,temp->E;t不是“可调用的”,即使是,A也是一种类型,因此作为参数没有意义。爬行,走路,然后跑。这些错误都与线程无关。 -
std::thread类型的对象没有operator(),因此表达式t(...)无效。试试std::thread t(A::d, a);? -
我已经抽象了所有这些细节以简化问题
-
@Charlie 效果很好。你知道我怎么能有一个线程数组吗?我尝试使用
std::thread my_threads[i](....)但它说Bad Array initializer?请添加您的答案,以便我接受 -
@rajkiran 作为答案添加了其他 cmets 以供您跟进。
标签: c++ multithreading c++11 pthreads