title: Function Pointer Array to a Method within a Class
body: <p>I have a class say Method with multiple functions for solving PDE's with different methods similar to the add, subtract, multiply... functions below. Another function in the class (DoMath in this examples) calls the methods. The user can change the method type (add, subtract,...), so I would like to create a Method pointer array (method_pointer), where the method is selected by choosing an integer "method_type". I have a version in C where everything is in one file without being members of a class, where this works fine; however, when the pointer is a member of the class I get "Method_pointer is not a function or function pointer" errors. I can't get the syntax correct, any help would be greatly appreciated. Below is a sample program of what I'm trying to do.</p>
  
  <p>Method.h Header</p>
  
  <pre><code>class Method{
  public:
      Method();
      ~Method();
  
  void add(int, int, int);
  void subtract(int, int, int);
  void multiply(int, int, int);
  void divide(int, int, int);
  
  void DoMath(int, int, int);
  
  void set_method( const int method_number);
  
  private:
     int method_type;
  };
  </code></pre>
  
  <p>Method.cc Source</p>
  
  <pre><code>#include "method.h"
  
  typedef void (*method_function)(int, int,int);
  
  Method::Method(){
      method_type=2;
  }
  
  Method::~Method(){
  }
  
  void Method::set_method( int method_number){
      method_type=method_number;
  }
  
  void Method::add( int a, int b, int c){
      c=a+b;
  }
  
  void Method::subtract( int a, int b, int c){
      c=a+b;
  }
  
  void Method::multiply( int a, int b, int c){
      c=a+b;
  }
  
  void Method::divide( int a, int b, int c){
      c=a+b;
  }
  
  void Method::DoMath(int x, int y, int result){
     method_function method_pointer[4]={add,subtract, multiply,divide};
     result=method_pointer[method_type](x,y);
  }
  </code></pre>
  
  <p>main.cc</p>
  
  <pre><code>int main(int argc, const char * argv[]) {
      Method *method;
      int x=2;
      int y=3;
      int result=0;
      Method.DoMath(x,y,result);
      return 0;
  }
  </code></pre>