Passing a function as a parameter

gcc v.8.3 -std=gnu++11

http://www.cplusplus.com/forum/general/27641/

I'm trying to pass a function as a parameter and failing. I've looked at the cplusplus and stackoverflow forums which are quite clear as to how to do what I'm failing to appreciate. It seems simple, until I get the error messages.


Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class MinimalSolver {
   typedef double (*func)(double sum, double char);
   void driver();
   double genX(func_t fnc);  
   double myThingie(double sum, double chr);
}

void MinimalSolver::driver() {
   genX(myThingie);
   // no matching function for call to 'MinimalSolver::genX(<unresolved overloaded function type>)'
   // note: double MinimalSolver::genX(MinimalSolver::func_t)
}
double MinimalSolver::genX(func_t fun) { }
double MinimalSolver::myThingie(double sum, double chr) { }
member functions are messages passed to objects. So you need an object to pass the message to.
when you do asdf.bar(42) the object `asdf' is passed as the this argument to the bar member function

In other words, when you write
1
2
3
class foo{
   void bar(int);
};
that would be equivalent to doing void bar(foo *this, int);

So the prototype for the function pointer is incorrect.
If myThingie will not use any MinimalSolver object, then don't make it a member function.
If it does, then change the signature of the function pointer to take a member function typedef double (MinimalSolver::*func)(double, double); (¿what the hell is double char?)
also, your new type (line 2) is "func", not "func_t", so lines 4 and 13 need to use "func" not "func_t" as the type.
Topic archived. No new replies allowed.