我正面临着一个大问题,三天来我一直试图解决这个问题,但都是徒劳的。我得到了一个CDS
类,其中包含一个intensity_func
成员函数和一个big_gamma
成员函数,后者基本上是成员intensity_func
函数的积分。
#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:
#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
函数(使用辛普森规则)。代码如下:
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();
}
};
因此,我希望在我的CDS.cpp
中定义big_gamma成员函数是:
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
。
发布于 2013-06-22 17:28:27
double (*fct)(double)
声明了一个“指向函数的指针”类型的参数。您需要将其声明为“指向成员的指针函数”:double (CDS::*fct)(double)
。此外,您还需要一个在其上调用指向成员的指针的对象:
(someObject->*fct)(someDouble);
https://stackoverflow.com/questions/17249174
复制相似问题