线程 std::调用未知类型,无法专门化函数错误

Thread std::invoke unknown type and failed to specialize function error

本文关键字:专门化 函数 错误 类型 std 调用 未知 线程      更新时间:2023-10-16

我尝试在函数中使用线程并将参数作为指针类发送,我接受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:Usersserhan.erkovansourcereposkkkkk_v2kkkkk_v2byte_decode.h(78,94): warning C4138: '*/' found outside of comment
1>C:Usersserhan.erkovansourcereposkkkkk_v2kkkkk_v2connect_tcp.cpp(45,3): warning C4551: function call missing argument list
1>D:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includethread(40,14): error C2672: 'std::invoke': no matching overloaded function found
1>D:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includethread(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 Studio2019CommunityVCToolsMSVC14.23.28105includethread(47): message :         with
1>D:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includethread(47): message :         [
1>D:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includethread(47): message :             _Tuple=_Tuple
1>D:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includethread(47): message :         ]
1>D:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includethread(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:Usersserhan.erkovansourcereposkkkkk_v2kkkkk_v2fake_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:Usersserhan.erkovansourcereposkkkkk_v2kkkkk_v2fake_main.cpp(291): message :         with
1>C:Usersserhan.erkovansourcereposkkkkk_v2kkkkk_v2fake_main.cpp(291): message :         [
1>C:Usersserhan.erkovansourcereposkkkkk_v2kkkkk_v2fake_main.cpp(291): message :             _Fn=int (__cdecl &)(SOCKET,int)
1>C:Usersserhan.erkovansourcereposkkkkk_v2kkkkk_v2fake_main.cpp(291): message :         ]
1>D:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includechrono(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 Studio2019CommunityVCToolsMSVC14.23.28105includechrono(177): message : see reference to class template instantiation 'std::chrono::duration<__int64,std::nano>' being compiled
1>D:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includechrono(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 Studio2019CommunityVCToolsMSVC14.23.28105includethread(36,5): error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'
1>D:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includetype_traits(1571): message : see declaration of 'std::invoke'
1>D:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includethread(36,5): message : With the following template arguments:
1>D:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includethread(36,5): message : '_Callable=int (__cdecl *)(SOCKET,int)'
1>D:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.23.28105includethread(36,5): message : '_Types={can *}'
1>Done building project "kkkkk_v2.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

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);