-4

I am getting this error while running this code. Please look in to my code and assist me.

#include "stdafx.h"
#include <iostream>
class A
{
  public:
    void PrintTwoNumbers(int (*numberSource)(void)) 
    {
      int val1= numberSource();     
    }

    int overNineThousand(void) 
    {
      return (rand()%1000) + 9001;
    }        
};

int _tmain(int argc, _TCHAR* argv[])
{ 
  int (A::*fptr) (void) = &A::overNineThousand;

  int (A::*fptr1) (void);
  fptr1 = &A::overNineThousand;

  A a;
  a.PrintTwoNumbers(&fptr); //-> how to pass here
  getchar();
  return 0; 
}

I am fed up by searching this online, and nobody is giving a perfect solution for this.Can anybody edit this code as working code and help me?

BLUEPIXY
  • 38,201
  • 6
  • 29
  • 68
shivcena
  • 1,723
  • 5
  • 22
  • 39
  • 1
    Non-static member functions are not the same as a static member function, a global function or a namespace function. Non-static member functions differ in that they need an object to be called on, which is passed invisibly to the function as the `this` pointer. I suggest you read about [`std::function`](http://en.cppreference.com/w/cpp/utility/functional/function) and [`std::bind`](http://en.cppreference.com/w/cpp/utility/functional/bind). Lastly, please don't spam with unrelated language tags. – Some programmer dude May 29 '17 at 13:37
  • Possible duplicate of [Using a C++ class member function as a C callback function](https://stackoverflow.com/questions/1000663/using-a-c-class-member-function-as-a-c-callback-function) – Algirdas Preidžius May 29 '17 at 13:38
  • @Someprogrammerdude : i am using visual studio 2008 , so std::function and std::bind are not available – shivcena May 29 '17 at 13:41
  • 1
    In addition: You are fed up searching for _perfect solution_ for this problem, but you never elaborated on what, said _perfect solution_, is. How did you expect us to know what the _perfect solution_ is to you, and then, provide it to you? – Algirdas Preidžius May 29 '17 at 13:41
  • 1
    @Martin: Adding "thanks" is not only _not_ a "grammar edit", but also inappropriate/unneeded/unwanted – Lightness Races in Orbit May 29 '17 at 13:42
  • If you don't have `std::function` then you need to use a pointer to a member function or a `static` member function (or global/namespace function) wrapper to be able to use pointer to non-member function in your `PrintTwoNumbers` function. – Some programmer dude May 29 '17 at 13:44
  • 1
    @shivcena [`std::function`](https://msdn.microsoft.com/en-us/library/bb982519(v=vs.90).aspx) **is** available on VS2008. Did you even **try** to research this topic? – Algirdas Preidžius May 29 '17 at 13:44
  • @AlgirdasPreidžius : i need to pass fptr as a parameter of class A , can you edit it as working code without using std::bind and std::function, because in visual studio 2008 don't have the provision for std::bind and std::function – shivcena May 29 '17 at 13:46
  • @shivcena Did you even follow the link to the documentation of `std::function` for VS2008, that I provided? Why are you saying that it's not available there, while, clearly, it is? – Algirdas Preidžius May 29 '17 at 13:48

1 Answers1

1

The expected argument is a (non-member) function pointer. You instead pass a (pointer to a) pointer to member function. (Pointers to) Pointers to member functions are not convertible to pointers to (non-member) functions.

Probably the simplest solution is to fix the function argument to be of correct type, pass the implicit object argument and don't take the address of your member function pointer when you call.

void PrintTwoNumbers(int (A::*numberSource) ()) 
{
  int val1= (this->*numberSource)();
}

a.PrintTwoNumbers(fptr);
eerorika
  • 181,943
  • 10
  • 144
  • 256
  • user2079303: it is working fine for above code,thanks, how to pass integer argument for this case, int overNineThousand(int a) { return a; } – shivcena May 29 '17 at 13:56
  • In the argument list parenthesis, just like in all invokations. – eerorika May 29 '17 at 13:57