传递成员函数,它将另一个成员函数和var转换为std :: thread

时间:2015-02-11 19:24:05

标签: multithreading c++11 winsock

我已经搜索了解决方案,但在尝试了大约10种方法后,我无法解决编译问题:

 functional(1149): error C2064: term does not evaluate to a function taking 2 arguments

这是我的代码的一部分。

pConnectedClients.push_back(new RemoteClient(hClientSocket, RemoteClientNumber));
if (pConnectedClients.size() > 1)
{
    for (int unsigned i = 0; i < pConnectedClients.size(); i++)
    {
        if (RemoteClientRec == pConnectedClients[i]->GetNumber())
        {
            SOCKET tmp = pConnectedClients[i]->GetSocket();
            std::thread MessageThread(&cRunServer::HandleConnection, hClientSocket, &cRunServer::pConnectedClients[i]);
            MessageThread.join();
        }
    }
}

我应该如何正确传递该功能?

我认为导致问题的是GetSocket()成员函数......

    std::vector<RemoteClient*> pConnectedClients;

修改

更多背景信息:

class cRunServer : public RemoteClient {
public:
    cRunServer();
    ~cRunServer();
    void RunServer();
    void Listen(SOCKET &listeningSocket);
    void HandleConnection(SOCKET hClientSocket, SOCKET hClientSocket2);

protected:
    // Variables
    void BindAndCreate(SOCKET &listeningSocket);
    SOCKET hListeningSocket = INVALID_SOCKET;
    sockaddr_in sockAddr;
    std::vector<RemoteClient *> pConnectedClients;
    // Dane remote clienta
    // Nr clienta
    int RemoteClientNumber = 0;
    // Nr odbiorcy
    int RemoteClientRec = 0;

    // Funkcja laczy ze soba dwoch klientow

    std::string GetHostDescription(const sockaddr_in &sockAddr);
    void SetServerSockAddr(sockaddr_in *pSockAddr, int portNumber);
    void HandleMessaging(SOCKET hClientSocket, SOCKET hClientSocket2);
    void GetClientInfo(SOCKET &s);
};

// somewhere in `cRunServer::RunServer()`:  

try {
    int iResult;
    Listen(hListeningSocket);
    // Akceptujemy polaczenie
    // rzutowanie konieczne, aby moc wpisac strukture sockadrr_in do struktury sockaddr
    // Przechowywanie adresu zdalnego hosta jest opcjonalne! #wow
    hClientSocket = accept(hListeningSocket, 0, 0); //, reinterpret_cast<sockaddr*>(&clientSockAddr),&clientSockSize);
    // Sprawdzamy bledy
    if (hClientSocket == INVALID_SOCKET)
        throw Exception("accept function failed.");
    std::cout << "accepted.\n";
    GetClientInfo(hClientSocket);
    pConnectedClients.push_back(new RemoteClient(hClientSocket, RemoteClientNumber));
    if (pConnectedClients.size() > 1) {
        for (int unsigned i = 0; i < pConnectedClients.size(); i++) {
            if (RemoteClientRec == pConnectedClients[i]->GetNumber()) {
                std::thread MessageThread(&cRunServer::HandleConnection, this, hClientSocket,
                                        &cRunServer::pConnectedClients[i]);
            }
        }
    }
}

catch (Exception e)

1 个答案:

答案 0 :(得分:0)

  

std::thread MessageThread(&cRunServer::HandleConnection, hClientSocket, &cRunServer::pConnectedClients[i]);

我猜测HandleConnectioncRunServer的非静态成员函数,而hClientSocket不是cRunServer所需的对象。

添加:

std::thread MessageThread(&cRunServer::HandleConnection, 
          this,
          hClientSocket, 
          &cRunServer::pConnectedClients[i]);

假设thiscRunServer*