我正在尝试为我的OpenCV应用程序创建一个线程抓取器。我无法弄清楚为什么这段代码不能编译。它给了我一个错误,我认为这意味着函数调用是错误的。但是,它通常与使用std :: thread启动线程的方式完全相同!我想使用std :: thread来完成它,因为它将提供更多与平台无关的兼容性,所以请不要告诉我使用特定于平台的库。我也希望这是基于STL的,所以没有Boost或DLib。在我的main.cpp中,我有一个工作线程应用程序,代码如下:
#include <iostream>
#include <fstream>
#include <string>
#include <thread>
#include <mutex>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#define read_failure_threshold 512
long grabbers_active = 0;
namespace dev
{
class grabber
{
private:
bool enabled = false;
std::mutex lock;
int capture_mode;
int capture_id;
unsigned long read_failures = 0;
std::string stream;
std::string grabber_name;
cv::Mat image;
public:
void grabber_t()
{
.......[unimportant code]........
}
grabber(std::string name, int captureMode, int captureId, std::string location)
{
.......[unimportant code]........
}
void start()
{
if(!enabled)
{
std::thread grabber_thread(grabber_t);
grabber_thread.detach();
}
enabled = true;
grabbers_active++;
}
cv::Mat getImage()
{
.......[unimportant code]........
}
};
}
[ERRORS:]
In file included from /media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/main.cpp:1:0:
/media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/template.hpp: In member function ‘void dev::grabber::start()’:
/media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/template.hpp:119:52: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>)’
std::thread grabber_thread(grabber_t);
^
/media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/template.hpp:119:52: note: candidates are:
In file included from /media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/template.hpp:4:0,
from /media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/main.cpp:1:
/usr/include/c++/4.8/thread:133:7: note: std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (dev::grabber::*)(); _Args = {}]
thread(_Callable&& __f, _Args&&... __args)
^
/usr/include/c++/4.8/thread:133:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘void (dev::grabber::*&&)()’
/usr/include/c++/4.8/thread:128:5: note: std::thread::thread(std::thread&&)
thread(thread&& __t) noexcept
^
/usr/include/c++/4.8/thread:128:5: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::thread&&’
/usr/include/c++/4.8/thread:122:5: note: std::thread::thread()
thread() noexcept = default;
^
/usr/include/c++/4.8/thread:122:5: note: candidate expects 0 arguments, 1 provided
make[2]: *** [CMakeFiles/build.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/build.dir/all] Error 2
make: *** [all] Error 2
错误日志也在代码的末尾。我担心的唯一错误是线程错误。其他的是简单的修复,但要求我让线程工作。 我在Ubuntu,使用g ++(Ubuntu 4.8.2-19ubuntu1)4.8.2。我的CMakeLists.txt中启用了C ++ 0x。一切都在那里完美运作
我的主要目标是找出我收到此错误的原因。我一直在谷歌搜索和尝试不同的技巧,但没有任何工作!
先谢谢您的帮助:)
答案 0 :(得分:2)
改变:
std::thread grabber_thread(grabber_t);
进入那个:
std::thread grabber_thread(&grabber::grabber_t, this);
grabber_t
是对非静态成员函数的引用,需要传递其地址,但是&grabber_t
不能正常工作,因为在获取其地址时必须明确限定成员函数的名称,因此结果为&grabber::grabber_t
。