线程std :: invoke未知类型并且无法专门化函数错误

时间:2019-12-16 08:31:44

标签: c++ multithreading

我试图在in函数中使用线程并将参数作为指针类发送,我遇到了Error C2672 'std::invoke': no matching overloaded function foundError C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'的错误

我进行了很多搜索,但只有我发现关于此错误的每个人都忘记了发送参数。我没有忘记,但我仍然遇到这些错误。

我的功能如下

功能监听

void Listen(can* _c) {
while (_c->s != INVALID_SOCKET)
    {
        auto t = thread( &connect_tcp::Recv_data,_c->connection, _c->s,ref(_c->response), _c->signals ); //ERROR IS HERE
        t.join();
    }
}

班级可以

class can {
public:
    Cotp_connection cotp;
    mms mms_obj;
    connect_tcp* connection;
    mms_response response;
    LinkedList** list;
    all_size_encoder size_encoder;
    SOCKET s;
    Signals* signals;


};

connect_tcp类

class connect_tcp
{
public:

    SOCKET ConnectWithTcp();
    int send_data(SOCKET s, LinkedList** list,int &j);
    connect_tcp();
    connect_tcp(char* ip, int set_port);
    void Close_tcp(SOCKET s);
    void Recv_data(SOCKET s,mms_response &response, Signals *signals);
    std::mutex mutex;

private:
    SOCKET server;
    SOCKADDR_IN addr;
    int port;
    WSADATA WSAData;


};

功能Recv_data

void connect_tcp::Recv_data(SOCKET s,mms_response &response,Signals *signals) {
    LinkedList** list = new LinkedList * [1000];
    uint8_t* buffer = new uint8_t [10000];
    Sleep(1000);
    std::lock_guard<std::mutex>guard(mutex);
    recv(s, (char*)buffer, 10000, 0);
    /*this->mutex.unlock();*/
    decode_bytes(response,buffer, list,signals);

}

auto t = thread( &connect_tcp::Recv_data,_c->connection, _c->s,ref(_c->response), _c->signals ); connect_tcp::Recv_data是我的成员函数,而_c->connection是我存储成员函数的对象,其他变量是函数的参数。 我尝试了所有看到和阅读的内容,但每次都遇到这些错误,我找不到解决的办法。 如果您想探索课堂而不是我的作品。我可以编辑这篇文章。

构建输出

1>------ Build started: Project: kkkkk_v2, Configuration: Debug x64 ------
1>fake_main.cpp
1>C:\Users\serhan.erkovan\source\repos\kkkkk_v2\kkkkk_v2\byte_decode.h(78,94): warning C4138: '*/' found outside of comment
1>C:\Users\serhan.erkovan\source\repos\kkkkk_v2\kkkkk_v2\connect_tcp.cpp(45,3): warning C4551: function call missing argument list
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(40,14): error C2672: 'std::invoke': no matching overloaded function found
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(47): message : see reference to function template instantiation 'unsigned int std::thread::_Invoke<_Tuple,0,1>(void *) noexcept' being compiled
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(47): message :         with
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(47): message :         [
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(47): message :             _Tuple=_Tuple
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(47): message :         ]
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(53): message : see reference to function template instantiation 'unsigned int (__cdecl *std::thread::_Get_invoke<_Tuple,0,1>(std::integer_sequence<unsigned __int64,0,1>) noexcept)(void *)' being compiled
1>C:\Users\serhan.erkovan\source\repos\kkkkk_v2\kkkkk_v2\fake_main.cpp(291): message : see reference to function template instantiation 'std::thread::thread<int(__cdecl &)(SOCKET,int),can*&,void>(_Fn,can *&)' being compiled
1>C:\Users\serhan.erkovan\source\repos\kkkkk_v2\kkkkk_v2\fake_main.cpp(291): message :         with
1>C:\Users\serhan.erkovan\source\repos\kkkkk_v2\kkkkk_v2\fake_main.cpp(291): message :         [
1>C:\Users\serhan.erkovan\source\repos\kkkkk_v2\kkkkk_v2\fake_main.cpp(291): message :             _Fn=int (__cdecl &)(SOCKET,int)
1>C:\Users\serhan.erkovan\source\repos\kkkkk_v2\kkkkk_v2\fake_main.cpp(291): message :         ]
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\chrono(669): message : see reference to class template instantiation 'std::chrono::duration<double,std::ratio<1,1>>' being compiled
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\chrono(177): message : see reference to class template instantiation 'std::chrono::duration<__int64,std::nano>' being compiled
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\chrono(647): message : see reference to class template instantiation 'std::chrono::time_point<std::chrono::steady_clock,std::chrono::steady_clock::duration>' being compiled
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(36,5): error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\type_traits(1571): message : see declaration of 'std::invoke'
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(36,5): message : With the following template arguments:
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(36,5): message : '_Callable=int (__cdecl *)(SOCKET,int)'
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(36,5): message : '_Types={can *}'
1>Done building project "kkkkk_v2.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

1 个答案:

答案 0 :(得分:1)

std::thread的第一个参数是可调用对象,其定义如下:

template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );

此可调用对象必须可以使用以下语法进行调用:

f()

但是在您的示例中,您正在将成员函数指针传递给std::thread的构造,并且调用成员函数指针的正确语法如下:

(calleeObject->*fp)()

因此,您不能将成员函数指针传递给std::thread的构造函数。解决此问题的简单方法是使用lambda作为可调用对象:

std::thread([=_c]()
{
    _c->connection->Recv_data(_c->s, ref(_c->response), _c->signals);
});

或者您可以使用std::mem_fn适配器,并将被调用对象也包含在参数列表中,就像在您自己的代码中所做的一样:

std::thread(std::mem_fn(&connect_tcp::Recv_data), _c->connection, _c->s, ref(_c->response), _c->signals);