【问题标题】:Resolving invalid use of non-static member function解决非静态成员函数的无效使用
【发布时间】: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


【解决方案1】:

使用pthread 库,参数函数不能是类的成员。 您必须在所有内容之外单独定义它。

argument of type "void *(myclass::*)()" is incompatible with parameter of type "void *(*)(void *)"C/C++(167)

如我所见,您使用的是 C++,我强烈建议您检查 C++ 多线程。 link 语法简单多了,一切都是在幕后使用pthread实现的。

【讨论】:

    猜你喜欢
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多