【问题标题】:cpp multipleThread: detach non-class type errorc++多线程:分离非类类型错误
【发布时间】:2017-03-05 10:22:39
【问题描述】:

我正在使用 cpp 编写一个带有 multiplethread 的程序,但我遇到了这样的编译器错误: 我的代码可以如下呈现:

    //A.hpp
    class ControleCam{
    public:
    ControleCam();
    ~ControleCam();
    };

    //A.cpp
    #include "A.hpp"
    ControleCam::ControleCam(){
    ...
    }
    ControleCam::~ControleCam(){
    ...
    }
    //B.cpp
    #include <A.hpp>
    int main(){
    std::thread turnCam(ControleCam());
    turnCam.detach();
    }

所以有人知道我在哪里做错了,我该怎么办?

【问题讨论】:

  • 下次最好将错误复制为文本并粘贴到此处,而不是图像。
  • 好的!抱歉,我认为这样会更清楚

标签: c++ linux multithreading compiler-errors detach


【解决方案1】:
std::thread turnCam(ControleCam());

您已经点击了 C++ 的 Most Vexing Parse。上面的声明没有将turnCam 声明为std::thread 对象。而是将threadCam 声明为返回std::thread 的函数。使用额外的一对括号或使用统一的大括号初始化语法。

std::thread turnCam{ControleCam()};

顺便说一句,您需要在您的班级中有一个重载的operator()(...) 才能使上述内容起作用。

【讨论】:

  • 感谢您的回答!!我已将其更改为“turnCam{ControleCam()}”它可以工作!
  • 我在 A.cpp 中有一个 operator(),我没有把它放在那里,因为我认为这不是出错的部分 ;)
  • @Elsa,没问题!那是你做的一件好事。我只是添加了那条线来掩盖它,以防万一。好吧,我很高兴你的问题得到了解决。问候! :-)
猜你喜欢
  • 2017-04-10
  • 1970-01-01
  • 2018-12-09
  • 2019-03-01
  • 2015-09-11
  • 2021-07-30
  • 2014-02-07
  • 2016-09-28
  • 2021-11-13
相关资源
最近更新 更多