将采用另一个成员函数和 var 的成员函数传递给 std::thread
Passing member function which takes another member function and var to a std::thread
我已经搜索了解决方案,但在尝试了大约 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)
std::thread MessageThread(&cRunServer::HandleConnection, hClientSocket, &cRunServer::pConnectedClients[i]);
我猜 HandleConnection
是 cRunServer
的非静态成员函数,而 hClientSocket
不是 cRunServer
的必需对象。
添加:
std::thread MessageThread(&cRunServer::HandleConnection,
this,
hClientSocket,
&cRunServer::pConnectedClients[i]);
假设 this
是 cRunServer*
。
见
- How to use boost bind with a member function
- how boost::function and boost::bind work
我已经搜索了解决方案,但在尝试了大约 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)
std::thread MessageThread(&cRunServer::HandleConnection, hClientSocket, &cRunServer::pConnectedClients[i]);
我猜 HandleConnection
是 cRunServer
的非静态成员函数,而 hClientSocket
不是 cRunServer
的必需对象。
添加:
std::thread MessageThread(&cRunServer::HandleConnection,
this,
hClientSocket,
&cRunServer::pConnectedClients[i]);
假设 this
是 cRunServer*
。
见
- How to use boost bind with a member function
- how boost::function and boost::bind work