0

I'm trying to create threads in a constructor of a class that will run a couple of functions inside that class, I've tried this:

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
            */
        }
    }
}

but it's giving me an errors: 'ServerLogic::dispatchMessage': function call missing argument list; use '&ServerLogic::dispatchMessage' to create a pointer to member.

IntelliSense: function "std::thread::thread(const std::thread &)" (declared at line 70 of "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\thread") cannot be referenced -- it is a deleted function.

etamar211
  • 53
  • 7
  • 3
    Did you confuse `dispatchThread` with `dispatchMessage`? – nwp Feb 29 '16 at 16:46
  • The error says it's all - this would be your fist fix. After you fixed that, you will have another error - see if you can fix it too. – SergeyA Feb 29 '16 at 16:55

1 Answers1

3

I think that the error message is basically telling you what to do. Consider the following code (which is problematic, but just serves to illustrate the point):

#include <thread>

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

    void bar()
    {

    }
};



int main()
{
    foo f;
}

To specify a member function, use

std::thread(&foo::bar, this);
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185