Invalid friend function identifiers

I work with OpenCV and Qt 5. I need to pass the mouse callback to namedwindow for some work that I am doing. However, I cannot make him see any of the private member variables of my class.

Here is the code:

class testWizard : public QWizard { Q_OBJECT public: testWizard(); ~testWizard(); friend void mouseHandler(int, int, int, void*); private: cv::Mat preview; bool drag; cv::Rect rect; }; 

Friend Function:

 void mouseHandler(int event, int x, int y, void* param) { cv::Point p1, p2; if(event == CV_EVENT_LBUTTONDOWN && !drag) { p1 = cv::Point(x,y); drag = true; } if(event == CV_EVENT_LBUTTONDOWN && drag) { cv::Mat temp; preview.copyTo(temp); } } 

I do not know what I am doing wrong. I am sure this is the right way to announce this. I am told that preview and drag and drop are undeclared identifiers. Unfortunately, I need to do this this way, since I need access to private members, and passing a pointer to a member function is not possible because of this hidden argument.

Can anyone help? Thanks!

+4
source share
1 answer

With a friend declaration, your function will have access to the members of the testWizard object. However, you still need to provide an object or a pointer to such an object to access the variables:

 testWizard* wizard = getTestWizard(); // no idea how to do that if(event == CV_EVENT_LBUTTONDOWN && !wizard->drag) { ... } 
+3
source

Source: https://habr.com/ru/post/1498788/


All Articles