我正面临着一个我一直试图解决的问题3天的大问题。我有CDS
类与intensity_func
成员函数和big_gamma
成员函数,它基本上是成员intensity_func
函数的组成部分。使用其他成员函数的C++成员函数
#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);
};
这里是CDS.cpp与intensity_func
成员函数的定义:
#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
。
但在我的main.cpp {返回罪( x);},它完美地工作。 – marino89
@ marino89 - 完全正确。 '窦'是一个普通的函数,你可以用指向函数来指向它。 '强度'是一个成员函数,你可以用指向成员函数指向它。 –
好的,所以我需要做的是:'双CDS :: big_gamma(双t) { double(CDS :: * fptr)(double); fptr =&CDS :: intensity_func; 返回积分(fprt,0,t,1000); };'。问题是我没有使用你所说的一个对象来调用指向成员的指针。我需要什么样的对象? – marino89