0

I have a class and it has a member function and i want to call that member function in a non member function.

void mouseEvent(int evt, int x, int y, int flags, void* param);
class CMainClass
{

private:
// Xcoordinate of the click event
     int Xcoordinate;
public:
// Xcoordinate get method
         int CMainClass::GetXcoordinate()
         {
          return Xcoordinate;
         }

// Xcoordinate set method
         void CMainClass::SetXcoordinate(int newVal)
         {
           CMainClass::Xcoordinate = newVal;
         }

};


void mouseEvent(int evt, int x, int y, int flags, void* param)
{
      CMainClass::SetXcoordinate(x);

}

C++ a nonstatic member reference must be relative to a specific object

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Ali
  • 141
  • 3
  • 12
  • If you have 10 different `CMainClass` objects, which of those do you expect the member function to be called on? – Max Langhof Sep 23 '19 at 07:19
  • Possible duplicate of [How can I pass a class member function as a callback?](https://stackoverflow.com/questions/400257/how-can-i-pass-a-class-member-function-as-a-callback) – Max Langhof Sep 23 '19 at 07:24
  • Make `CMainDialog::SetXcoordinate()` static – I. Hamad Sep 23 '19 at 07:35
  • The code you show is also invalid C++. You keep changing between `CMainClass` and `CMainDialog`, and `int CMainDialog::GetXcoordinate()` is not a valid function declaration inside a class. – Max Langhof Sep 23 '19 at 07:36
  • I made the changes and I have only one CMainClass object. – Ali Sep 23 '19 at 10:25

1 Answers1

0

ignoring non-relevant issues with your code, you are calling a member function without creating an object.

In mouseEvent, you need to use an object to call SetXcoordinate.

void mouseEvent(int evt, int x, int y, int flags, void* param)
{
    //CMainClass obj();
    obj.SetXcoordinate(x);
}

The object creation doesn't have to be in the same function if you created it elsewhere.

xrplorer
  • 1
  • 1