【发布时间】:2020-12-21 14:17:47
【问题描述】:
这是我的代码,请告诉我如何解决此错误。这两个函数都是类的方法。
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***
【问题讨论】:
-
请用标题传达具体问题,而不是笼统地谈论您遇到问题的原因。
-
注意:本题与线程无关。这是一个关于如何在需要指向普通函数的指针的函数调用中提供指向成员函数的指针作为 arg 的问题。
-
另请注意:从新的 C++ 程序调用 Posix 线程库 (pthreads) 是个坏主意。您应该改用
std::thread类。 en.cppreference.com/w/cpp/thread
标签: c++ multithreading operating-system