This question already has answers here:
C++ Thread taking reference argument failed compile
(2个答案)
去年关闭。
我正在尝试做这样的事情:
当我尝试构建它时,会出现一些错误:
在/ usr / include / c ++ / 6 / thread:39:0包含的文件中,
从src / rpi_server / rpiserver.cpp:11:
/ usr / include / c ++ / 6 / functional:在“ struct std :: _ Bind_simple”的实例中:
/ usr / include / c ++ / 6 / thread:138:26:要求来自“ std :: thread :: thread(_Callable &&,_Args && ...)[with _Callable = void(*&)(int,RPiServer&); _Args = {int&,RPiServer&}]’
src / rpi_server / rpiserver.cpp:89:79:从这里需要
/ usr / include / c ++ / 6 / functional:1365:61:错误:“类std :: result_of”中没有名为“ type”的类型
typedef类型名result_of :: type result_type;
^ ~~~~~~~~~~~
/ usr / include / c ++ / 6 / functional:1386:9:错误:“类std :: result_of”中没有名为“ type”的类型
_M_invoke(_Index_tuple )
^ ~~~~~~~~~
Makefile:29:目标“ build / rpi_server / rpiserver.o”的配方失败
当我以以下方式更改线程功能时(删除对对象的引用):
一切都建立得很好。将引用作为参数传递给线程函数时,我做错了什么?
您收到的错误是因为没有为
附注:同样,您复制粘贴的代码示例错误,复制了
(2个答案)
去年关闭。
我正在尝试做这样的事情:
void commands_conn_handler(int socket, RPiServer& server) {
// Not important code about handling connection
}
class RPiServer {
public:
void Accept(void Accept(void (*acceped_conn_handler)(int, RPiServer&)) {
// (...)
int remote_socket = 0; // Doesn't matter - example.
std::thread conn_handler_thread(acceped_conn_handler, remote_socket, *this);
conn_handler_thread.join();
}
};
int main() {
RPiServer commands_server();
commands_server.Accept(commands_conn_handler);
}
当我尝试构建它时,会出现一些错误:
在/ usr / include / c ++ / 6 / thread:39:0包含的文件中,
从src / rpi_server / rpiserver.cpp:11:
/ usr / include / c ++ / 6 / functional:在“ struct std :: _ Bind_simple”的实例中:
/ usr / include / c ++ / 6 / thread:138:26:要求来自“ std :: thread :: thread(_Callable &&,_Args && ...)[with _Callable = void(*&)(int,RPiServer&); _Args = {int&,RPiServer&}]’
src / rpi_server / rpiserver.cpp:89:79:从这里需要
/ usr / include / c ++ / 6 / functional:1365:61:错误:“类std :: result_of”中没有名为“ type”的类型
typedef类型名result_of :: type result_type;
^ ~~~~~~~~~~~
/ usr / include / c ++ / 6 / functional:1386:9:错误:“类std :: result_of”中没有名为“ type”的类型
_M_invoke(_Index_tuple )
^ ~~~~~~~~~
Makefile:29:目标“ build / rpi_server / rpiserver.o”的配方失败
当我以以下方式更改线程功能时(删除对对象的引用):
void commands_conn_handler(int socket) {
// Not important code about handling connection
}
class RPiServer {
public:
void Accept(void (*acceped_conn_handler)(int)) {
// (...)
int remote_socket = 0; // Doesn't matter - example.
std::thread conn_handler_thread(acceped_conn_handler, remote_socket);
conn_handler_thread.join();
}
};
int main() {
RPiServer commands_server();
commands_server.Accept(commands_conn_handler);
}
一切都建立得很好。将引用作为参数传递给线程函数时,我做错了什么?
最佳答案
因此,这里有一个有效的示例:
#include <thread>
#include <functional>
class RPiServer;
void commands_conn_handler(int socket, RPiServer &server) {
// Not important code about handling connection
}
class RPiServer {
public:
void Accept(void (*acceped_conn_handler)(int, RPiServer&)) {
// (...)
int remote_socket = 0; // Doesn't matter - example.
std::thread conn_handler_thread(acceped_conn_handler, remote_socket, std::ref(*this));
conn_handler_thread.join();
}
};
int main() {
RPiServer commands_server;
commands_server.Accept(commands_conn_handler);
}
您收到的错误是因为没有为
conn_handler_thread
的构造函数提供正确的类型。要显式获取对对象的引用(您需要在此处执行),请使用std::ref()
函数。附注:同样,您复制粘贴的代码示例错误,复制了
void Accept
部分。您在main()
中也出现了most vexing parse错误。