簡體   English   中英

解決無效使用非靜態成員 function

[英]Resolving invalid use of non-static member function

這是我的代碼,請告訴我如何解決此錯誤。 這兩個函數都是 class 的方法。

    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庫,參數 function 不能是 class 的成員。 您必須在所有內容之外單獨定義它。

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

如我所見,您使用的是C++ ,我強烈建議您檢查 C++ 多線程。 鏈接語法簡單多了,一切都是在幕后使用pthread實現的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM