Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
std::Invoke,未找到匹配的重载函数 我试图创建一个线程,它使用C++中的套接字处理客户机-服务器通信。_C++_Multithreading_Std - Fatal编程技术网

std::Invoke,未找到匹配的重载函数 我试图创建一个线程,它使用C++中的套接字处理客户机-服务器通信。

std::Invoke,未找到匹配的重载函数 我试图创建一个线程,它使用C++中的套接字处理客户机-服务器通信。,c++,multithreading,std,C++,Multithreading,Std,程序抛出一个错误 std::Invoke,未找到匹配的重载函数 错误C2893未能专门化函数模板“未知类型std::invoke(_Callable&&,_Types&&…)noexcept()” 我无法调试程序,因为它在启动时崩溃 使用两个参数进行线程初始化时是否有任何错误?还是我缺少一些库、类导入 有人能帮我吗,我还缺什么 这是我的密码 #include <ws2tcpip.h> #include <iostream> #include <string>

程序抛出一个错误

std::Invoke,未找到匹配的重载函数

错误C2893未能专门化函数模板“未知类型std::invoke(_Callable&&,_Types&&…)noexcept()”

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

使用两个参数进行线程初始化时是否有任何错误?还是我缺少一些库、类导入

有人能帮我吗,我还缺什么


这是我的密码

#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;
}```
#包括
#包括
#包括
#包括
使用名称空间std;
#pragma注释(lib,“Ws2_32.lib”)
#定义默认值\u BUFLEN 512
PCSTR IP_地址=“192.168.1.100”;
#定义默认_端口“3504”
结构客户端类型
{
插座;
int-id;
char接收到消息[默认值];
};
int进程客户端(客户端类型和新客户端);
int main();
int进程客户端(客户端类型和新客户端)
{
而(1)
{
memset(new_client.received_message,0,默认_BUFLEN);
if(new_client.socket!=0)
{
int iResult=recv(new_client.socket,new_client.received_消息,默认值为0);
如果(iResult!=套接字错误)

我可以把你的程序归结为:

#包括
结构客户端类型
{
};
int进程客户端(客户端类型和新客户端)
{
返回0;
}
int main()
{
客户端类型客户端;
std::线程我的线程(进程客户端,客户端);
}
由于您提到的错误,这个小片段无法编译

为什么会失败?让我们看看。在注释部分,我们发现:

线程函数的参数按值移动或复制。如果 引用参数需要传递给线程函数,它具有 包裹(例如,使用std::ref或std::cref)


实际上,
std::threadmythread(进程\客户端,std::ref(客户端));
编译没有问题。

仍然不起作用。我正在向线程发送一个成员函数,如
&className::WorkerThread
。使用
std::ref
包装引用,但仍然得到相同的结果error@ShuvoSarker如果你想使用一个成员函数作为线程函数,你必须这样做:哇!但是看起来就像一个可怕的设计。
#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);
}