我的情况如下,我有两个不同的二等分函数,它们将在我的代码中的某个时刻被调用。基本上,一些函数调用 Bisection2 并且该函数调用传递的函数或将函数指针传递给 Bisection 函数。
在标题中我有
std::vector<double> F();
double F1(double m1, double m2);
double F2(double m1, double m2);
typedef double (MyClass::*MyClassFn)(double,double);
double Bisection(MyClassFn fEval,double min, double max,std::vector<double> args);
bool Bisection2(MyClassFn fEval1,MyClassFn fEval2,double xmin, double xmax, double ymin, double ymax,double *ax, double *ay,std::vector<double> args);
我的二分函数看起来像这样。我没有包含所有代码,因为它没有必要。
double MyClass::F1(double m1, double m2) {
m_m1 = m1;
m_m2 = m2;
F();
return m_my;
}
double MyClass::F2(double m1, double m2) {
m_m1 = m1;
m_m2 = m2;
F();
return m_mx;
}
double MyClass::Bisection(MyClass fEval,double min, double max,std::vector<double> args)
{
// Setting a lot of stuff here, including auxiliary and leftvalue...
MyClass *pObj = new MyClass(-1);
leftvalue = pObj->*fEval(auxiliary, left);
ightvalue = pObj->*fEval(auxiliary, right);
// Comparing and setting values here etc.
}
bool MyClass::Bisection2(MyClassFn fEval1,MyClassFn fEval2,double xmin, double xmax, double ymin, double ymax,double *ax, double *ay,std::vector<double> args)
{
// Setting some values here but these have nothing to do with the problem.
double yl;
double leftvalue, rightvalue, middlevalue;
MyClass *pObj = new MyClass(-1);
// Setting some values here but these have nothing to do with the problem.
std::vector <double> arg;
// pushing some values
yl = Bisection(fEval2,ymin,ymax,arg); // Here is the first way how I need to pass fEval2 to Bisection function.
arg.clear();
if(isnan(yl))
{
return M_NAN;
}
leftvalue = pObj->fEval1(xl, yl); // And here is the second way how I need to use fEval1.
//.....
}
然后我基本上有一个函数调用
`二等分2(F1,F2, m_m2,0.0, 0.0, m_max2, &m_mu1, &m_mu2,args);
Bisection2(...) 调用目前可能不正确,因为自从上次工作以来我已经更改了很多函数。上次我基本上直接在函数内部调用 F1 和 F2 函数指针而不是 fEval,但我很确定这是不正确的方式,毕竟我什至认为它似乎以某种方式工作。
现在 leftvalue = pObj->*fEval(auxiliary, left); 导致编译错误:
error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘fEval (...)’, e.g. ‘(... ->* fEval) (...)’
我试图从这里查看帮助http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.2 并且还检查了这些论坛中可能解决的不同问题,但仍然可以'不知道我做错了什么。
谢谢你。