Pointer to function to member function

≡放荡痞女 提交于 2019-12-04 12:26:32

You can't do that directly. You are trying to pass a pointer-to-member-function as a pointer-to-function. The way to go is to write a wrapper function (a normal function) which, for example, delegates the calculation to member function of your object, which is, for example, a global object.

EDIT

Actually, you are lucky: the second parameter to opt.set_min_objective is a pointer to your data which will be passed to the function you pass pointer to as first parameter. This means that your wrapper function doesn't have to use a global object.

class YourClass
{
public:
    double f(unsigned int i, const double*, double*, void*);
};

double wrapper(unsigned int i, const double* a, double* b, void* o) {
    // not sure what you'd need the last param for, but you say you have it...
    reinterpet_cast<YourClass*>(o)->f(i, a, b, o);
}

and then:

YourClass myP;
opt.set_min_objective(wrapper, &myP);

Another edit. Someone else had a similar problem before you: http://www.cplusplus.com/forum/general/73166/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!