【问题标题】:std::Invoke, No matching overloaded function foundstd::Invoke,找不到匹配的重载函数
【发布时间】:2020-09-22 23:50:08
【问题描述】:

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

程序报错

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

错误 C2893 无法专门化函数模板 'unknown-type 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;
}```

【问题讨论】:

    标签: c++ multithreading std


    【解决方案1】:

    我将你的程序简化为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);
    }
    

    这个小sn-p编译失败,尝试编译出现你提到的错误。

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

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

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

    【讨论】:

    • 还是不行。我正在向&amp;className::WorkerThread 之类的线程发送一个成员函数。用std::ref 包装了引用,但仍然得到相同的错误
    • @ShuvoSarker 如果你想使用成员函数作为线程函数,你必须这样做:stackoverflow.com/questions/10673585/…
    • 哇!但看起来像一个糟糕的设计。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 2018-08-01
    • 2018-10-19
    • 1970-01-01
    相关资源
    最近更新 更多