title: function pointers generate 'invalid use of non-static member function' error
body: <p>I am trying to grasp pointer function concept in a better way. So I have a very simple and working example as:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  int add(int first, int second)
  {
      return first + second;
  }
  
  int subtract(int first, int second)
  {
      return first - second;
  }
  
  int operation(int first, int second, int (*functocall)(int, int))
  {
      return (*functocall)(first, second);
  }
  
  int main()
  {
      int  a, b;
      int  (*plus)(int, int);
      int  (*minus)(int, int);
      plus = &amp;add;
      minus = &amp;subtract;
      a = operation(7, 5, add);
      b = operation(20, a, minus);
      cout &lt;&lt; "a = " &lt;&lt; a &lt;&lt; " and b = " &lt;&lt; b &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>So far so good,
  Now I need to group the functions in a class, and select add or subtract based on the function pointer that i use. So I just make a small modification as:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  class A
  {
  public:
  int add(int first, int second)
  {
      return first + second;
  }
  
  int subtract(int first, int second)
  {
      return first - second;
  }
  
  int operation(int first, int second, int (*functocall)(int, int))
  {
      return (*functocall)(first, second);
  }
  };
  
  int main()
  {
      int  a, b;
      A a_plus, a_minus;
      int (*plus)(int, int) = A::add;
      int (*minus)(int, int) = A::subtract;
      a = a_plus.operation(7, 5, plus);
      b = a_minus.operation(20, a, minus);
      cout &lt;&lt; "a = " &lt;&lt; a &lt;&lt; " and b = " &lt;&lt; b &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>and the obvious error is:</p>
  
  <pre><code>ptrFunc.cpp: In function ‘int main()’:
  ptrFunc.cpp:87:29: error: invalid use of non-static member function ‘int A::add(int, int)’
  ptrFunc.cpp:88:30: error: invalid use of non-static member function ‘int A::subtract(int, int)’
  </code></pre>
  
  <p>coz I haven't specified which object to invoke(and I don't want to use static methods for now)</p>
  
  <p><strong>EDIT:</strong>
  several comments and answers suggested that the non-static version(as I have written) is not possible.(thanks to all)
  So,
  Modifying the class in the following manner also wont work:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  class A
  {
      int res;
  public:
      A(int choice)
      {
          int (*plus)(int, int) = A::add;
          int (*minus)(int, int) = A::subtract;
          if(choice == 1)
              res = operation(7, 5, plus);
          if(choice == 2)
              res = operation(20, 2, minus);
          cout &lt;&lt; "result of operation = " &lt;&lt; res;
      }
  int add(int first, int second)
  {
      return first + second;
  }
  
  int subtract(int first, int second)
  {
      return first - second;
  }
  
  int operation(int first, int second, int (*functocall)(int, int))
  {
      return (*functocall)(first, second);
  }
  };
  
  int main()
  {
      int  a, b;
      A a_plus(1);
      A a_minus(2);
      return 0;
  }
  </code></pre>
  
  <p>generated this error:</p>
  
  <pre><code>ptrFunc.cpp: In constructor ‘A::A(int)’:
  ptrFunc.cpp:11:30: error: cannot convert ‘A::add’ from type ‘int (A::)(int, int)’ to type ‘int (*)(int, int)’
  ptrFunc.cpp:12:31: error: cannot convert ‘A::subtract’ from type ‘int (A::)(int, int)’ to type ‘int (*)(int, int)’
  </code></pre>
  
  <p>may I know how to solve this issue please?</p>
  
  <p>thanks</p>