0

In c++ how do i refer a member function of a class to a non member function which is further being called by another member function of that class? This is my code and it return into this error:

[Error] cannot convert 'void (example::*)()' to 'void (*)(int)' for argument '2' to 'int non_member_func(int, void (*)(int))'
#include<iostream>
int non_member_func(int,void func(int))
{
    return 0;    
}

class example
{
public:
    void member_func1()
    {
        
    }
    void member_func2(int x)
    {
        int num = non_member_func(1,&this->member_func1);
    }
    
}object;

int main()
{
    object.member_func1();
    return 0;    
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Tariqul
  • 1
  • 2
  • When making a [mcve], please make sure it only replicates the error or problem you're actually asking about, and don't have other unrelated problems or errors. Please [edit] your question to show an actual [mcve]. Also please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Aug 05 '20 at 10:07
  • 1
    Does this answer your question? [Function pointer to member function](https://stackoverflow.com/questions/2402579/function-pointer-to-member-function) – UnholySheep Aug 05 '20 at 10:08
  • As for your problem, you can't do what you want. Not only because you use the incorrect way to pass a pointer to a member function (you need to use the class-name and the scoping operator, as in `&example::member_func1`), a pointer to a (non-static) member function can not be passed as a pointer to a non-member function. Non-static member functions have an extra hidden argument that becomes the `this` pointer inside it, something non-member functions doesn't have. – Some programmer dude Aug 05 '20 at 10:09
  • To solve your problem, please do some research about [`std::function`](https://en.cppreference.com/w/cpp/utility/functional/function) (or depending on use-case, templates) and [lambdas](https://en.cppreference.com/w/cpp/language/lambda). – Some programmer dude Aug 05 '20 at 10:10

0 Answers0