2016-02-29 168 views
0

我試圖將運行一對夫婦的那類的內部函數的類的構造函數來創建線程,我已經試過這樣:線程在構造函數,C++

ServerLogic::ServerLogic(SOCKET sock) 
{ 
    this->sock = sock; 
    this->dispatchThread = new std::thread(this->dispatchMessage); 

} 

void ServerLogic::dispatchMessage(){ 
    /* 
    * this function will handle the connetions with the clients 
    */ 
    char recievedMsg[1024]; 
    int connectResult; 
    //receive data 
    while (true){ 
     connectResult = recv(this->sock, recievedMsg, sizeof(recievedMsg), 0); 
     //in case everything good send to diagnose 
     if (connectResult != SOCKET_ERROR){ 
      this->messagesToDiagnose.push(std::string(recievedMsg)); 
     } 
     else{ 
      /* 
      * destructor 
      */ 
     } 
    } 
} 

,但它給我一個錯誤: 'ServerLogic :: dispatchMessage':函數調用缺少參數列表;使用'& ServerLogic :: dispatchMessage'來創建一個指向成員的指針。 (在C:\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ VC \ include \ thread的第70行中聲明的智能感知函數「std :: thread :: thread(const std :: thread &)」) 「)不能被引用 - 它是一個被刪除的函數。

+3

你混淆'dispatchThread'用'dispatchMessage'? – nwp

+0

是的,請參閱更新 – etamar211

+0

錯誤說這是所有 - 這將是你的拳頭解決。修復之後,您將會遇到另一個錯誤 - 請參閱是否可以修復它。 – SergeyA

回答

3

我認爲這個錯誤信息基本上是告訴你該怎麼做。考慮下面的代碼(這是個問題,但只是用來說明這一點):

#include <thread> 

class foo 
{ 
public: 
    foo() 
    { 
     std::thread(&foo::bar, this); 
    } 

    void bar() 
    { 

    } 
}; 



int main() 
{ 
    foo f; 
} 

要指定一個成員函數,使用

std::thread(&foo::bar, this);