我有一个成员函数“ myfunction”和“ my约束1”,如下所示。
//f= k1(x1-a1)^2 + k2(x2-a2)^2 + k3(x3-a3)^2
double CNonlinearOptimization::myfunction(unsigned n, const double *x, double *grad, void *pdata) {
if (grad) {
grad[0] = 2*k1*(x[0]-1);
grad[1] = 2*k2*(x[1]-3);
grad[2] = 2*k3*(x[2]-5);
}
return (k1*(x[0]-1)*(x[0]-1)+k2*(x[1]-3)*(x[1]-3)+k3*(x[2]-5)*(x[2]-5));
}
double CNonlinearOptimization::myconstraint1(unsigned n, const double *x, double *grad, void *data){
++count;
if (grad) {
grad[0] = -1; //grad[0] = d(c1)/dx1
grad[1] = 0; //grad[1] = d(c1)/dx2
grad[2] = 0; //grad[2] = d(c1)/dx3
}
return (0.9-(x[0]-2));
}
i将通过在主函数中应用nlopt来调用其函数,如下所示。
nlopt_set_min_objective(opt,nonlinopt.myfunction, NULL);
nlopt_add_inequality_constraint(opt,nonlinopt.myconstraint1, NULL, 1e-8);
我试图做一个指针:
CNonlinearOptimization nonlinopt;
nlopt_set_min_objective(opt,nonlinopt.myfunction, NULL);
但是,我遇到了一些错误:
NonlinearOptimization.cpp(288): error C3867: 'CNonlinearOptimization::myfunction': function call missing argument list; use '&CNonlinearOptimization::myfunction' to create a pointer to member
NonlinearOptimization.cpp(290): error C3867: 'CNonlinearOptimization::myconstraint1': function call missing argument list; use '&CNonlinearOptimization::myconstraint1' to create a pointer to member
有人知道如何解决吗?
答案 0 :(得分:0)
如错误消息所述,您应该使用
&objectName.memberFunctionName
语法以获得指向成员函数的指针。请注意&
。
尝试通过这种方式传递
nlopt_set_min_objective(opt, &nonlinopt.myfunction, NULL);