C 11螺纹 - 非静态成员功能的使用无效 - 工作GCC 5.1.0损坏的GCC 7.3.1
C++11 Threading - invalid use of non-static member function - works gcc 5.1.0 broken gcc 7.3.1
代码:
// create class instance
MC_THREAD MTHR;
// set some values in the class
MTHR.setup_mc_thread("com6", &es);
// create an thread to run the non-static member function
std::thread MCTHR(MC_THREAD::start_mc_thread, std::ref(MTHR));
该函数的定义是:
void MC_THREAD::start_mc_thread(){
while(1){
//do_stuff
}
}
Windows 8&10使用基于GCC 5.1.0的TDM-GCC编译器。
以上代码在Linux上使用GCC 7.3生成以下错误:
错误:非静态成员函数的使用无效'void mc_thread :: start_mc_thread((’|
生成错误的机器使用GCC(GCC(7.3.1 20180303(红色帽子7.3.1-5(
运行Fedora 27任何建议都将不胜感激!
已解决:请参阅下面的Sam评论
也必须链接pthread。
我最初认为问题是std::reference_wrapper
的用法。但是,汇编错误实际上是由于类成员函数需要是指针。以下用GCC 7.3.1编译:
#include <thread>
#include <utility>
#include <functional>
class x {
public:
void start_thread();
};
int main()
{
x y;
std::thread thr{&x::start_thread, std::ref(y)};
}
为类成员启动线程的常用方法是将普通指针作为类实例传递。在这种情况下,这也有效:
std::thread thr{&x::start_thread, &y};
这是更常见的语法。不需要参考包装器。但是在所有情况下,第一个参数必须是指向类方法的指针。&x::start_thread
是这样的指针。x::start_thread
本身不是。那是真正的问题。
std::thread
的构造函数的参数仅指定为...
f-可呼叫的对象要在新线程中执行
您必须真正挖掘可可对象的定义。我想到的基本用法对应于在此列出的第三个选项。但是参考包装器也可以接受。但是,在所有情况下,您都必须将指针指向成员函数作为第一个参数。
相关文章:
- 为什么会发生堆损坏
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 奇怪的结构&GCC&clang(void*返回类型)
- GCC本机矩阵运算库
- PowerPC ppc64le上的Gcc Woverloaded虚拟错误
- gcc和c++17的过载解析失败
- 数据成员SFINAE的C++17测试:gcc vs clang
- GCC对可能有效的代码抛出init list生存期警告
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 平均图像时图像损坏
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 使用gcc从静态链接的文件中查找可选符号
- 普通环路未使用gcc 4.8.5自动矢量化
- 有了gcc,是否可以链接库,但前提是它存在
- 在clang++预处理器中确定gcc工具链版本
- C 11螺纹 - 非静态成员功能的使用无效 - 工作GCC 5.1.0损坏的GCC 7.3.1
- 由值传递的结构,在 C -> C++ 回调函数期间损坏 (gcc 4.1)