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_intensity
및 this->m_paytimes
을 더 이상 사용할 수 없습니다. Invalid use of this outside a non-static member function
.
하지만 내 MAIN.CPP에를 {(죄를 반환 x);}, 그것은 완벽하게 작동합니다. – marino89
@ marino89 - 정확합니다. '부비동 (sinus) '은 보통의 함수이고 포인터는 함수를 가리킨다. 'intensity'는 멤버 함수이고, 여러분은 포인터를 멤버 함수로 가리키고 있습니다. –
좋아, 그래서해야 할 일은 다음과 같다 :'double CDS :: big_gamma (double t) { double (CDS :: * fptr) (double); fptr = & CDS :: intensity_func; 통합 반환 (fprt, 0, t, 1000); }; 문제는 포인터를 멤버로 호출하는 객체를 말한 것처럼 사용하지 않는다는 것입니다. 어떤 종류의 대상이 필요합니까? – marino89