c++ 多线程:分离 non-class 类型错误
cpp multipleThread: detach non-class type error
我正在使用 cpp 使用 mutiplethread 编写一个程序,但我有一个像这样的 compiler-error :
我的代码可以如下所示:
//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
被声明为 returns 一个 std::thread
的函数。使用额外的一对括号或使用统一的大括号初始化语法。
std::thread turnCam{ControleCam()};
顺便说一句,您的 class 中需要有一个超载的 operator()(...)
才能使上述工作正常。
我正在使用 cpp 使用 mutiplethread 编写一个程序,但我有一个像这样的 compiler-error :
//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
被声明为 returns 一个 std::thread
的函数。使用额外的一对括号或使用统一的大括号初始化语法。
std::thread turnCam{ControleCam()};
顺便说一句,您的 class 中需要有一个超载的 operator()(...)
才能使上述工作正常。