下面是我的代码,请告诉我如何解决这个错误。这两个函数都是类的方法。
void* make_desc(void* arg){
int *k;
k = (int *) arg;
Node* tt;
int i,j;
//for(int k=0;k<4;k++){
int c[16],t[2];
//tr is in the begin of the code, used for going, up,down,left or rigth
i=this->zero[0]+tr[(*k)].first;
j=this->zero[1]+tr[(*k)].second;
if(i>=0 && i<4 && j>=0 && j<4){
cp(this->array,c);
c[this->zero[0]*4+this->zero[1]]=c[i*4+j];
c[i*4+j]=0;
t[0]=i;
t[1]=j;
//check if note already seen, if not seen add
//if seen but depth is smaller than previous, add also
if(setx.find(myhash(c))==setx.end()){
Node *tt = new Node(c,t,this->depth+1,this->path);
//l.push_back(tt);
}
else if(setx[myhash(c)]>this->depth+1){
setx[myhash(c)]=this->depth+1;
Node *tt = new Node(c,t,this->depth+1,this->path);
//l.push_back(tt);
}
}
pthread_exit(tt);
//return tt;
// return l;
// }
}
vector<Node*> threads(){
vector<Node*> l;
Node* ret;
int j=0;
for(int i=0;i<4;i++){
j=i;
pthread_create(&th[i],NULL,make_desc,(void*)&j);
}
//错误
main.cpp: In member function ‘void Node::Make_threads()’:
main.cpp:208:54: error: invalid use of non-static member function ‘void* Node::make_desc(void*)’
pthread_create(&th[i],NULL,make_desc,NULL);
^
main.cpp:169:11: note: declared here
void* make_desc(void* arg) {
^~~~~~~~~
main.cpp: In function ‘bool A_star_Manhattan()’:
main.cpp:272:58: error: no matching function for call to ‘Node::make_desc()’
vector<Node *> dsc = current_node->make_desc();
^
main.cpp:169:11: note: candidate: void* Node::make_desc(void*)
void* make_desc(void* arg) {
^~~~~~~~~
main.cpp:169:11: note: candidate expects 1 argument, 0 provided***
使用pthread
库时,参数函数不能是类的成员。你必须在一切之外单独定义它。
argument of type "void *(myclass::*)()" is incompatible with parameter of type "void *(*)(void *)"C/C++(167)
正如我所看到的,您正在使用C++,我强烈建议您检查C++多线程。链接的语法要简单得多,所有的东西都是在幕后使用pthread
实现的。