c++ - How can I use a function as argument to a member function? -
i trying use member function sover::tov argument function using std::bind. compile error @ calling std::bind. briefly, essence of code following. wrong syntax or there better way want?
class solver { private: .... public: .... double solve(vector< vector<double> > & sol, const double &z, const double &n) {... method(std::bind(&solver::tov, *this, pl::_1 , pl::_2 , pl::_3 ),y,x,dx) ...} void tov ( const vector<double> &y, vector<double> &dy, const double &x ) {...} }; void method(void (*i_function)(const vector< double > &, vector<double> &, const double &), vector< double > &y, const double &x, const double &h) {...}
let me give concrete example reproduces main problem:
void method(void (*i_function)(double &), double &y) { i_function(y); } class solver { public: solver() {} void solve(double &y) { method(do_calc,y); } void do_calc(double &x) { x += 1; } }; int main() { double y = 1; solver().solve(y); std::cout << y << std::endl; }
which gives error: error: cannot convert ‘solver::do_calc’ type ‘void (solver::)(double&)’ type ‘void (*)(double&)’ void solve(double &y) { method(do_calc,y); }
even if use
void method(std::function<void(double &)> i_function, double &y) { i_function(y); }
it doesn't work.
Comments
Post a Comment