在OpenCV中试图将滑动条回调函数与变量进行封装,便于管理,尝试以下类似代码时出错,error C3867。
cv::createTrackbar ("Thresh", "HarrDemo", &this->thresh, this->maxThresh, this->HarrTrack);
//或者
cv::createTrackbar ("Thresh", "HarrDemo", &object.thresh, object.maxThresh, object.HarrTrack);
将 this->HarrTrack前面加上&会出现error C2276: “&”: 绑定成员函数表达式上的非法操作?
原因:类的成员函数默认带有一个this指针参数,那么它作为泛函的参数其实就不匹配了,因为泛函中的Func类型并没有this指针。
解决思路:用全局函数、友元函数或者类静态成员函数作为回调函数,利用了函数creatTrackbar的最后一个参数,函数原型:
@param onChange Pointer to the function to be called every time the slider changes position. This function should be prototyped as void Foo(int,void\*); , where the first parameter is the trackbar
position and the second parameter is the user data (see the next parameter). If the callback is the NULL pointer, no callbacks are called, but only value is updated.
@param userdata User data that is passed as is to the callback. It can be used to handle trackbar events without using global variables.
*/
CV_EXPORTS int createTrackbar(const String& trackbarname, const String& winname,