0

我正面临一个大问题,我已经尝试解决了 3 天。我有一个带有成员函数的CDS类和一个成员函数,它基本上是成员函数的组成部分。intensity_funcbig_gammaintensity_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;
    }

我在另一个源文件中实现了一个函数来集成函数和成员函数中使用index_betaintensity_func函数(使用辛普森规则)。这是代码:

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_intensitythis->m_paytimes因为我收到以下错误消息:Invalid use of this outside a non-static member function.

4

1 回答 1

4

double (*fct)(double)声明一个“指向函数”类型的参数。您需要将其声明为“指向成员函数” double (CDS::*fct)(double):此外,您需要一个在其上调用指向成员的对象:

(someObject->*fct)(someDouble);
于 2013-06-22T09:28:26.927 回答