首页 > 解决方案 > 从不同的 cpp 调用回调函数会导致 bad_function_call

问题描述

我目前正在开发一个 OpenCV 项目,我想从不同的 cpp 调用 main.cpp 中的回调函数。
我面临的问题是,当我想调用该函数时,我得到一个 bad_function_call,因为回调函数为空。

我的 main.cpp 看起来像这样:

 PointDetection *detect;
 detect->houghCallback = [hPublisher] (std::vector<cv::Vec4i> data) {
        //do something
 };

我的 PointDetector.hpp,它包含在 PointDetector.cpp 中:

 std::function <void (std::vector<cv::Vec4i> data) houghCallback;

我调用函数的 PointDetector.cpp 如下所示:

 void PointDetector::houghDetectCb(int status, void *userData) {
        std::vector<cv::Vec4i> houghPointsResult;
        PointDetector *self = static_cast<PointDetector *>(userData);    //This includes every variable in the hpp except the houghCallback for some reason 
        self->houghCallback(houghPointsResult);                          //This causes the bad_function_call because houghCallback is Null 
 }

有人知道是否可以像这样调用回调?

标签: c++opencvcallback

解决方案


我找到了解决方案:我在实际 GPU 操作之前调用了 stream.enqueqeHostCallback (请参阅此问题:cuda 流:流执行后未调用回调


推荐阅读