gpt4 book ai didi

c++ - 错误 C2064 : term does not evaluate to a function taking 1 arguments - something about threads

转载 作者:行者123 更新时间:2023-11-30 05:13:07 29 4
gpt4 key购买 nike

我遇到了这个错误:

error C2064: term does not evaluate to a function taking 1 arguments

我不知道为什么,但我发现它与线程有关(如果我注释掉有关它编译的线程的行)。问题出在以下几行:

thread t(&TriviaServer::clientHandler, this->_socket);

和:

thread t(&TriviaServer::clientHandler, client_socket);

来自函数 serve() 和 acceptClient。

我的代码:

#include "TriviaServer.h"

#define PORT 8820
#define IFACE 0


TriviaServer::TriviaServer()
{
this->_db = DataBase();
this->_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (this->_socket == INVALID_SOCKET)
throw exception(__FUNCTION__ " - socket");
}

TriviaServer::~TriviaServer()
{
for (unsigned int i = 0; i < this->_connectedUsers.size(); i++)
delete this->_connectedUsers[i];
this->_connectedUsers.clear();
for (unsigned int i = 0; i < this->_roomList.size(); i++)
delete this->_roomList[i];
this->_roomList.clear();
TRACE(__FUNCTION__ " closing accepting socket");
try
{
closesocket(this->_socket);
}
catch (exception e)
{
cout << e.what() << endl;
}
catch (...) {}
}

void TriviaServer::serve()
{
bindAndListen();

thread t(&TriviaServer::clientHandler, this->_socket);
t.detach();

while (true)
{
TRACE("accepting client...");
acceptClient();
}
}

void TriviaServer::bindAndListen()
{
struct sockaddr_in sockAddr = { 0 };
sockAddr.sin_port = PORT;
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = IFACE;

if (::bind(this->_socket, (struct sockaddr*)&sockAddr, sizeof(sockAddr)) == SOCKET_ERROR)
throw exception(__FUNCTION__ " - bind");
TRACE("binded");

if (::listen(this->_socket, SOMAXCONN) == SOCKET_ERROR)
throw exception(__FUNCTION__ " - listen");
TRACE("listening...");
}

void TriviaServer::acceptClient()
{
SOCKET client_socket = accept(this->_socket, NULL, NULL);
if (client_socket == INVALID_SOCKET)
throw exception(__FUNCTION__);

TRACE("Client accepted !");

thread t(&TriviaServer::clientHandler, client_socket);
t.detach();
}

void TriviaServer::clientHandler(SOCKET)
{
//TODO
}

有人知道原因以及如何解决吗?

最佳答案

thread t(&TriviaServer::clientHandler, this->_socket);

由于 &TriviaServer::clientHandler 定义为:

TriviaServer::clientHandler(SOCKET);

通过函数指针传递给调用的第一个参数 &TriviaServer::clientHandler类型的隐式this参数>TriviaServer,或者指向TriviaServer对象的指针。您需要在函数声明采用的任何其他参数之前传递它。所以你想做:

thread t(&TriviaServer::clientHandler, this, this->_socket);

关于c++ - 错误 C2064 : term does not evaluate to a function taking 1 arguments - something about threads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44091628/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com