美文网首页
错误:对‘std::thread::thread(

错误:对‘std::thread::thread(

作者: 瓜尔佳Anthony | 来源:发表于2021-07-09 16:42 被阅读0次

    在类中定义了成员函数report()以及run(),现在想在run()函数中创建线程运行report()函数,原始代码如下:

    class ClassA
    {
        ...
        void run();
        void report();
        ...
    };
    ...
    void ClassA::run()
    {
        thread t_report(report);
        t_report.detach();
    }
    

    编译报错:

    错误:对‘std::thread::thread(<unresolved overloaded function type>)’的调用没有匹配的函数

    经查有两点需要注意,首先不能只提供成员函数而不提供类型,即需要传递&ClassA::report以指示成员函数的地址,其次非静态成员函数需要明确对其进行调用的对象。代码修改为:

    class ClassA
    {
        ...
        void run();
        void report();
        ...
    };
    ...
    void ClassA::run()
    {
        thread t_report(&ClassA::report, this);
        t_report.detach();
    }
    

    编译通过。
    参考stackoverflow:std :: thread :: thread(<unresolved overloaded function type>)在Qt中 - VoidCC

    相关文章

      网友评论

          本文标题:错误:对‘std::thread::thread(

          本文链接:https://www.haomeiwen.com/subject/vcbfpltx.html