首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用其他成员函数的C++成员函数

使用其他成员函数的C++成员函数
EN

Stack Overflow用户
提问于 2013-06-22 17:24:08
回答 1查看 1.4K关注 0票数 0

我正面临着一个大问题,三天来我一直试图解决这个问题,但都是徒劳的。我得到了一个CDS类,其中包含一个intensity_func成员函数和一个big_gamma成员函数,后者基本上是成员intensity_func函数的积分。

代码语言:javascript
运行
复制
#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:

代码语言:javascript
运行
复制
#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函数(使用辛普森规则)。代码如下:

代码语言:javascript
运行
复制
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成员函数是:

代码语言:javascript
运行
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-22 17:28:27

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

代码语言:javascript
运行
复制
(someObject->*fct)(someDouble);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17249174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档