2016-08-23 80 views
0

我的代碼不能編譯,並且無法得到它的原因。是否因爲我以錯誤的方式使用newton_raphson_iterateC++ Boost在類成員函數上使用newton_raphson_iterate

我需要使用綁定嗎?歡迎任何關於如何在成員類函數中使用牛頓拉夫遜的例子。

class MyB{ 

    struct funct{ 
     double target; 
     double DerivativePrecision; 
     int mo; 
     bool isG; 
     MyB* bond; 
    public: 
     funct(double target_, double DerivativePrecision_, int mo_, bool isG_, MyB* bond_) : 
      target(target_), DerivativePrecision(DerivativePrecision_), mo(mo_), isG(isG_), bond(bond_) 
     {} 
     std::tr1::tuple<double,double> operator()(const double& x) const { 
      double localYtP = bond->yTp(x, mo, isG); 
      return std::tr1::make_tuple ( 
       localYtP - target, 
       (bond->yTp(x+DerivativePrecision,mo, isG)-localYtP)/DerivativePrecision 
      ); 
     } 
    };  

    public: 
     /* 
     .....   
     */ 
     double yTp(double x, int mo, int isG); 

     double pty(double p, int mo, int isG){ 
      funct localFunc(p, 0.000001, mo, isG, this); 
      double y = boost::math::tools::newton_raphson_iterate(localFunc(p), 
                    0.1, 
                    -0.1, 
                    0.4, 
                    std::numeric_limits<double>::digits 
                    ); 
      return y; 
     }   
} 

int main() 
{ 
    system("pause"); 
    return 0; 
} 

我得到兩個錯誤消息:

第一:

\boost/math/tools/roots.hpp(202) : error C2064: term does not evaluate to a function taking 1 arguments 

指向該代碼的最後一行(BOOST):

template <class F, class T> 
T newton_raphson_iterate(F f, T guess, T min, T max, int digits, boost::uintmax_t& max_iter) 
{ 
    BOOST_MATH_STD_USING 

    T f0(0), f1, last_f0(0); 
    T result = guess; 

    T factor = static_cast<T>(ldexp(1.0, 1 - digits)); 
    T delta = 1; 
    T delta1 = tools::max_value<T>(); 
    T delta2 = tools::max_value<T>(); 

    boost::uintmax_t count(max_iter); 

    do{ 
     last_f0 = f0; 
     delta2 = delta1; 
     delta1 = delta; 
     boost::math::tie(f0, f1) = f(result); 
... 

第二:

see reference to function template instantiation 'T boost::math::tools::newton_raphson_iterate<std::tr1::tuple<_Arg0,_Arg1>,double>(F,T,T,T,int)' being compiled 

指向(在我的課),以

double y = boost::math::tools::newton_raphson_iterate(localFunc(p), 
                    0.1, 
                    -0.1, 
                    0.4, 
                    std::numeric_limits<double>::digits 
                    ); 
+0

*「我的代碼不能編譯」*:編譯器說什麼?什麼是錯誤和哪些行? – wasthishelpful

+1

你不應該將參數傳遞給'newton_raphson_iterate'' localFunc(p)',它是函子operator()的調用,而是'localFunc',它實際上是你的函數。 'newton_raphson_iterate'需要6個參數:你錯過了一個。 – wasthishelpful

回答

0

的問題是你想何時調用newton_raphson_iterate所以最終傳遞funct作爲第一個模板參數,這是不可調用的調用localFunc。你應該直接通過localFunc

newton_raphson_iterate(localFunc, 0.1, -0.1, 0.4, std::numeric_limits<double>::digits);