我的情況如下,我有兩個不同的平分函數,在我的代碼中的某個點將被調用。基本上有些函數調用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.
//.....
}
然後,我已經基本功能什麼叫
`Bisection2(F1,F2,m_m2,0.0,0.0,m_max2,& m_mu1,& m_mu2,參數);
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 還檢查了這些論壇,也許不同的解決問題,但仍然無法弄清我做錯了。
謝謝。
在'Bisection'參數'的*定義* fEval'被聲明爲具有'MyClass'類型。這是不正確的。它應該是'MyClassFn',因爲它在'Bisection'的聲明中。 – AnT