2013-06-22 3 views
0

3 일 동안 헛되이 해결하려고 노력한 큰 문제에 직면 해 있습니다. 저는 intensity_func 멤버 함수와 멤버 함수를 가진 CDS 클래스를 가지고 있습니다. 멤버 함수는 기본적으로 멤버 intensity_func 함수의 정수입니다. 기능을 통합하는 기능을 파일 내가 다른 소스에서 구현 한다른 멤버 함수를 사용하는 C++ 멤버 함수

#include <vector> 
#include <random> 
#include <cmath> 

#include "CDS.h" 

double CDS::intensity_func(double t) 
{ 
    vector<double> x = this->m_intensity; 
    vector<double> y = this->m_paytimes; 
    if(t >= y.back() || t< y.front()) 
    { 
     return 0; 
    } 

    else 
    { 
     int d=index_beta(y, t) - 1; 
     double result = x.at(d) + (x.at(d+1) - x.at(d))*(t - y.at(d))/ (y.at(d+1) - y.at(d)); 
     return result; 
    } 

intensity_func에 사용되는 index_beta 기능 :

#include <vector> 
#include <cmath> 

using namespace std 

class CDS 
{ 
public: 
    CDS(); 
    CDS(double notional, vector<double> pay_times, vector<double> intensity); 
    ~CDS(); 


double m_notional; 
vector<double> m_paytimes; 
vector<double> m_intensity; 

double intensity_func(double); 

double big_gamma(double); 

}; 

그리고 여기에는 intensity_func 멤버 함수의 정의와 CDS.cpp입니다 멤버 함수 (Simpson의 규칙을 사용).

double simple_integration (double (*fct)(double),double a, double b) 
{ 
     //Compute the integral of a (continuous) function on [a;b] 
     //Simpson's rule is used 
     return (b-a)*(fct(a)+fct(b)+4*fct((a+b)/2))/6; 
}; 


double integration(double (*fct)(double),double a, double b, double N) 
{ 
     //The integral is computed using the simple_integration function 
     double sum = 0; 
     double h = (b-a)/N; 
     for(double x = a; x<b ; x = x+h) { 
      sum += simple_integration(fct,x,x+h); 
     } 
     return sum; 
}; 

int index_beta(vector<double> x, double tau) 
{ 
    // The vector x is sorted in increasing order and tau is a double 


    if(tau < x.back()) 
    { 
     vector<double>::iterator it = x.begin(); 
     int n=0; 

     while (*it < tau) 
     { 
      ++ it; 
      ++n; // or n++; 
     } 
     return n; 
    } 

    else 
    { 
     return x.size(); 
    } 


}; 

그래서, 내가이 big_gamma 멤버 함수를 정의하는 내 CDS.cpp에하고 싶은됩니다 :

double CDS::big_gamma(double t) 
{ 
    return integration(this->intensity, 0, t); 
}; 

그러나 분명히, 다음이 작동하지 않고 내가 얻을 여기에 코드입니다 오류 메시지 : reference to non static member function must be called. 그런 다음 intensity 멤버 함수를 정적 함수로 바꾸려고 시도했지만 새로운 문제가 발생했습니다. 다음과 같은 오류 메시지가 나타날 때 this->m_intensitythis->m_paytimes을 더 이상 사용할 수 없습니다. Invalid use of this outside a non-static member function.

답변

4

double (*fct)(double) "포인터 - 함수"유형의 인수를 선언합니다. 대신 "포인터에 대한 멤버 함수"로 선언해야합니다 : double (CDS::*fct)(double). 또한, 당신은 당신이 포인터 - 투 - 멤버를 호출하는 객체가 필요합니다 예를 들어 부비동 기능 이중 동 (더블 X)과의 통합 기능을 사용할 때

(someObject->*fct)(someDouble); 
+0

하지만 내 MAIN.CPP에를 {(죄를 반환 x);}, 그것은 완벽하게 작동합니다. – marino89

+0

@ marino89 - 정확합니다. '부비동 (sinus) '은 보통의 함수이고 포인터는 함수를 가리킨다. 'intensity'는 멤버 함수이고, 여러분은 포인터를 멤버 함수로 가리키고 있습니다. –

+0

좋아, 그래서해야 할 일은 다음과 같다 :'double CDS :: big_gamma (double t) { double (CDS :: * fptr) (double); fptr = & CDS :: intensity_func; 통합 반환 (fprt, 0, t, 1000); }; 문제는 포인터를 멤버로 호출하는 객체를 말한 것처럼 사용하지 않는다는 것입니다. 어떤 종류의 대상이 필요합니까? – marino89