【问题标题】:Passing member function which takes another member function and var to a std::thread将接受另一个成员函数和 var 的成员函数传递给 std::thread
【发布时间】:2015-04-12 07:42:10
【问题描述】:

我已经寻找解决方案,但在尝试了大约 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)

【问题讨论】:

  • cRunServer::HandleConnection 的声明是什么? hClientSocket是什么类型的?
  • 有点讽刺的是,在你添加的所有代码中,你仍然设法省略了解释 cRunServer 实例来自哪里的部分(我们只能 /guess/ try 块是否在该类的成员函数...)
  • 我的错,对不起。它是 void cRunServer::RunServer() 成员函数。所有函数都是 CRunServer 或 RemoteClient 类的成员

标签: multithreading c++11 winsock


【解决方案1】:

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

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

添加:

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

假设thiscRunServer*

【讨论】:

  • @reybanger 你在说什么?这证实了我确实是对的。
  • HandleConnection 有 2 个套接字作为参数:void HandleConnection(SOCKET hClientSocket, SOCKET hClientSocket2);
  • 对不起,我是新手,我不能很好地格式化commants,再给我一个:)
  • @reybanger 没有人可以很好地格式化 cmets,因为这不是他们的目的。代码进入问题。但是,您不需要在评论中发布代码。只是说/问你想说/问什么
  • @reybanger 重点是,all 非静态成员函数具有隐含的this 参数。使用bind 时,您必须将其作为第一个参数显式传递。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 2014-02-20
相关资源
最近更新 更多