I get an error on the following code. It seems I can´t use a function which belongs to a class as an argument for "genericfunction".
Any help on how to overcome the problem would be great.
Thanks.

//Main
int main()
{
 teste teste_classe;

// this works fine  
 cout << teste_classe.genericfunc(1.0,2.0,sum_2)<< endl;

// this returns an error saying function is not of type double(*)(double,double) !!! 
 cout << teste_classe.genericfunc(1.0,2.0, teste_classe.sum  )<< endl;
 
 system("pause");
 return 0;
};

//Header

double sum_2(double a, double b);

class teste 
{
 public:

 double sum(double x, double y);
  
 double genericfunc(double x, double y, double (*f)(double x, double y) );       
};

//Cpp

#include "teste.hpp"

double sum_2(double a, double b)
{
 return a+b;
};

double teste::sum(double x, double y)
{
 return x+y;
};      

double teste::genericfunc(double x, double y, double (*f)(double x, double y))
{
 return f(x,y); 
};

So functors are the way to go? Never would have guessed!
Thanks for your reply and the reference to the tutorial.

Hmm, as I re-read your code your "generic function" seems to me to be very un-generic --at least in the C++ sense of the word. Hence my suggestion for functors.

If you really want to use function pointers directly you will have to figure some other way to fix it. Otherwise you will need to implement it in terms of C++ generics. Here's an example:

#include <iostream>
using namespace std;

//----------------------------------------
// This is our functor class.
//----------------------------------------
// Currently it is rather simplistic as the point of a functor is
// to hold state, and this class is stateless (it has no member
// variables). In essence, it is just a callable object.
//
class ftor {
  public:
    int operator () ( int augend, int addend ) {
      return augend + addend;
      }
  };

//----------------------------------------
// Here is a normal, everyday function.
//----------------------------------------
int func( int augend, int addend ) {
  return augend + addend;
  }

//----------------------------------------
// Here is a generic function.
//----------------------------------------
// That's "generic" in the C++ sense of the word.
// The function is used below with two different
// types of arguments, so the compiler actually
// creates two functions.
//
template <typename predicate>
void do_something( predicate f, std::string info ) {
  cout << "10 + 5 = "
       << f( 10, 5 )
       << " (" << info << ')'
       << endl;
  }

//----------------------------------------
// Examples of using the generic function
//----------------------------------------
int main() {
  do_something( func,   "using a regular function" );
  do_something( ftor(), "using a temporary functor" );
  ftor inst;
  do_something( inst,   "using a locally instantiated functor" );
  }

Hope this makes things a little clearer with functors. Alas, I think I didn't really answer your original problem.

This question has already been answered. Start a new discussion instead.