gpt4 book ai didi

c++ - std::Invoke,找不到匹配的重载函数

转载 作者:行者123 更新时间:2023-12-03 12:53:55 28 4
gpt4 key购买 nike

我正在尝试创建一个线程,该线程使用 C++ 中的套接字处理客户端-服务器通信。

程序抛出错误

std::Invoke, No matching overloaded function found

Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'



我无法调试程序,因为它在启动时崩溃。

有两个参数的线程初始化有什么问题吗?或者我错过了一些图书馆,类导入?

任何人都可以帮助我,我在这里缺少什么?

这是我的代码
#include <ws2tcpip.h>
#include <iostream>
#include <string>
#include <thread>

using namespace std;

#pragma comment (lib, "Ws2_32.lib")

#define DEFAULT_BUFLEN 512
PCSTR IP_ADDRESS = "192.168.1.100";

#define DEFAULT_PORT "3504"

struct client_type
{
SOCKET socket;
int id;
char received_message[DEFAULT_BUFLEN];
};

int process_client(client_type& new_client);
int main();

int process_client(client_type& new_client)
{
while (1)
{
memset(new_client.received_message, 0, DEFAULT_BUFLEN);

if (new_client.socket != 0)
{
int iResult = recv(new_client.socket, new_client.received_message, DEFAULT_BUFLEN, 0);

if (iResult != SOCKET_ERROR)
cout << new_client.received_message << endl;
else
{
//cout << "recv() failed: " << WSAGetLastError() << endl;
break;
}
}
}

if (WSAGetLastError() == WSAECONNRESET)
cout << "The server has disconnected" << endl;

return 0;
}

int main()
{
WSAData wsa_data;
struct addrinfo* result = NULL, * ptr = NULL, hints;
string sent_message = "";
client_type client = { INVALID_SOCKET, -1, "" };
int iResult = 0;
string message;

cout << "Starting Client...\n";

// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsa_data);
if (iResult != 0) {
cout << "WSAStartup() failed with error: " << iResult << endl;
return 1;
}

ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

cout << "Connecting...\n";

// Resolve the server address and port
iResult = getaddrinfo(IP_ADDRESS, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
cout << "getaddrinfo() failed with error: " << iResult << endl;
WSACleanup();
system("pause");
return 1;
}

// Attempt to connect to an address until one succeeds
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {

// Create a SOCKET for connecting to server
client.socket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (client.socket == INVALID_SOCKET) {
cout << "socket() failed with error: " << WSAGetLastError() << endl;
WSACleanup();
system("pause");
return 1;
}

// Connect to server.
iResult = connect(client.socket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(client.socket);
client.socket = INVALID_SOCKET;
continue;
}
break;
}

freeaddrinfo(result);

if (client.socket == INVALID_SOCKET) {
cout << "Unable to connect to server!" << endl;
WSACleanup();
system("pause");
return 1;
}

cout << "Successfully Connected" << endl;

//Obtain id from server for this client;
recv(client.socket, client.received_message, DEFAULT_BUFLEN, 0);
message = client.received_message;

if (message != "Server is full")
{
client.id = atoi(client.received_message);

thread my_thread(process_client, client);

while (1)
{
getline(cin, sent_message);
iResult = send(client.socket, sent_message.c_str(), strlen(sent_message.c_str()), 0);

if (iResult <= 0)
{
cout << "send() failed: " << WSAGetLastError() << endl;
break;
}
}

//Shutdown the connection since no more data will be sent
my_thread.detach();
}
else
cout << client.received_message << endl;

cout << "Shutting down socket..." << endl;
iResult = shutdown(client.socket, SD_SEND);
if (iResult == SOCKET_ERROR) {
cout << "shutdown() failed with error: " << WSAGetLastError() << endl;
closesocket(client.socket);
WSACleanup();
system("pause");
return 1;
}

closesocket(client.socket);
WSACleanup();
system("pause");
return 0;
}```

最佳答案

我把你的程序归结为 minimal, reproducible example :

#include <thread>

struct client_type
{
};

int process_client(client_type& new_client)
{
return 0;
}

int main()
{
client_type client;

std::thread my_thread(process_client, client);
}
这个小片段无法编译,尝试编译会出现您提到的错误。
为什么这会失败?让我们看看 std::thread constructor .在注释部分,我们发现:

The arguments to the thread function are moved or copied by value. Ifa reference argument needs to be passed to the thread function, it hasto be wrapped (e.g., with std::ref or std::cref).


确实 std::thread my_thread(process_client, std::ref(client));编译没有问题。

关于c++ - std::Invoke,找不到匹配的重载函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62187098/

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