Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;11线程-非静态成员功能的无效使用-工程gcc 5.1.0 gcc 7.3.1_C++_C++11_Compiler Errors_Stdthread - Fatal编程技术网

C++ C++;11线程-非静态成员功能的无效使用-工程gcc 5.1.0 gcc 7.3.1

C++ C++;11线程-非静态成员功能的无效使用-工程gcc 5.1.0 gcc 7.3.1,c++,c++11,compiler-errors,stdthread,C++,C++11,Compiler Errors,Stdthread,代码: 该功能的定义是: // 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::sta

代码:

该功能的定义是:

    // 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)); 
上述代码使用基于GCC 5.1.0的TDM-GCC编译器在windows 8和10上编译(并正常工作)

以上代码在linux上使用gcc 7.3.1生成以下错误:
错误:非静态成员函数“void MC_THREAD::start_MC_THREAD()”的使用无效

产生错误的机器正在使用gcc(gcc)7.3.1 20180303(Red Hat 7.3.1-5)运行fedora 27

如有任何建议,将不胜感激

已解决:请参见下面山姆的评论


还必须链接pthread。

我最初认为问题在于
std::reference\u包装器的使用。但是编译错误实际上是由于类成员函数需要是指针。以下内容根据gcc 7.3.1进行编译:

    void MC_THREAD::start_mc_thread(){ 
        while(1){
            //do_stuff
        }
    } 
这是更常见的语法。实际上并不需要引用包装器。但在所有情况下,第一个参数必须是指向类方法的指针<代码>&x::start_thread
就是这样一个指针<代码>x::开始线程本身不是。这才是真正的问题

std::thread
的构造函数的参数

f-要在新线程中执行的可调用对象


你必须真正深入研究。我想到的基本用法对应于此处列出的可调用对象的第三个选项。但是引用包装器也是可以接受的。但是,在所有情况下,必须将指向成员函数的指针作为第一个参数。

使用
std::thread
执行类方法要求第一个参数是普通指针,即
this
,而不是引用。将参数更改为simply
&MTHR
@sam请将其作为答案。@tripkinetics--这不是正确的答案。
#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};