我正在使用 cpp 编写一个带有 mutiplethread 的程序,但是我遇到了这样的编译器错误: 我的代码可以如下所示:
//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();
}
所以有人知道我哪里做错了,我该怎么办?
最佳答案
std::thread turnCam(ControleCam());
您已达到 C++ 的 Most Vexing Parse .上面的声明没有将 turnCam
声明为 std::thread
对象。 threadCam
被声明为返回 std::thread
的函数。使用额外的一对括号或使用统一的大括号初始化语法。
std::thread turnCam{ControleCam()};
顺便说一句,你需要在你的类中有一个重载的 operator()(...)
才能使上述工作正常。
关于c++ - C++ 多线程 : detach non-class type error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42607145/