I’m writing a program with mutiplethread using cpp , but I have a compiler-error like this:
my code could be presented as follow:
//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();
}
So anybody have a clue about where I did wrong and what can I do?
Advertisement
Answer
std::thread turnCam(ControleCam());
You’ve hit C++’s Most Vexing Parse. The above declaration doesn’t declare a turnCam as a std::thread object. Rather threadCam is declared as a function that returns a std::thread. Use an extra pair of parenthesis or use uniform brace initialization syntax.
std::thread turnCam{ControleCam()};
BTW, you will need to have an overloaded operator()(...) in your class for the above to work.