mercredi 7 janvier 2015

How do I pass a function as a parameter

Win 7 cygwin gcc 4.8.3 -std=gnu++11


I've researched this on stackoverflow and cplusplus and thought that I'd passed a function as a parameter correctly, even 'elegantly', but it's clear I have misunderstood something. Can anyone explain how to pass a function as an argument correctly, and why what I have won't work?


A single warning is given for the prototype and a single error for the invocation. Changing genX to genY in the comments but both genX and genY have the same warnings/errors.



class MyClass {
typedef double (*func_t)(double sum);
void driver();
double genX(func_t fnc) { };
double genY(double (*funct)(double sum)) { };
// note: no known conversion for argument 1 from
// '<unresolved overloaded function type>' to
// MyClass::func_t {aka double (*)(double)}
double myThingie(double sum) { };
};

void MyClass::driver() {
genX(myThingie);
genY(myThingie);
// no matching function for call to 'MyClass::genX(<unresolved overloaded function type>)'
}

Aucun commentaire:

Enregistrer un commentaire