简体   繁体   English

class呼叫会员function无匹配function

[英]No matching function for call to a member function in class

I am new to C++ and trying to make a program to calculate bond price, the original code works well but I have difficulties transferring it to OOP. mode.我是 C++ 的新手,正在尝试制作一个计算债券价格的程序,原始代码运行良好,但我很难将其转移到 OOP.mode。 The program uses two arrays and a integer to do calculation.程序用两个arrays和一个integer做计算。 I used a loop in constructor to initialize data members (learned from stack over flow).我在构造函数中使用了一个循环来初始化数据成员(从 stack over flow 中学习)。 it looks fine but I experienced one error like: no matching function for call to member function. the data can't be passed to member function. I was trapped here a whole day.看起来不错,但我遇到一个错误,如:没有匹配function呼叫成员function。数据无法传递给成员function。我被困在这里一整天。 Could anybody give me some insights?有人可以给我一些见解吗? Thank you.谢谢你。 The code follows:代码如下:

#include <array>


#ifndef DRAFT_H
#define DRAFT_H


    class Draft
{
    public:

        Draft(int, double [], double[]);




        double F (double);
        void Bcalculator (int, double[], double[]);

        void printResult();
        void printDfactor();


    private:
        double discF[3]{};
        double bPrice {0};
        double bDuration {0};
        double bConvexity {0};

        double term[3];
        double cFlow[3];

        int sizeofArray;

    private:
};

#endif // DRAFT_H


#include "Draft.h"
#include <iostream>
#include <cmath>
#include <array>
#include <iomanip>
using namespace std;


    Draft::Draft( int arraySize, double termArr[], double cFlowArr[]):sizeofArray{arraySize}{

        for (int i = 0; i < 3; i++){

        term[i] = termArr[i];
        cFlow[i] = cFlowArr[i];}

}




    double Draft::F (double x){
        return  0.05 / (1 + exp(-pow((1 + x),2)));
        }



    void Draft::Bcalculator(int sizeofArray, double term[], double cFlow[]){



        double a = 0;
        int n = 16;

        for (int k =0; k < sizeofArray; k++){

        double h = (term[k] - a)/n;

        double x[n], fx[n];

        for (int i = 0; i <= n; i++){
            x[i] = a + i * h;
            fx[i] = F(x[i]);
        }

        double result = 0;
        double discF[]{};

        for (int i = 0; i <= n; i ++){
            if (i == 0 || i == n){
                result += fx[i];
            }
            else if (i % 2 != 0){
                result += 4 * fx[i];
            }
            else {
                result += 2 * fx[i];
            }

        }
        result = result * (h/3);

        discF[k] = exp (- result);
        bPrice += discF[k] * cFlow[k];
        bDuration += term[k] * cFlow[k] * discF[k];
        bConvexity += pow(term[k], 2) * cFlow[k] * discF[k];

}
        bDuration = bDuration / bPrice;
        bConvexity = bConvexity / bPrice;


}

    void Draft::printDfactor(){
        for (int k = 0; k < sizeofArray; k++) {

            cout << k + 1 << setw (20) << discF[k] << endl;
        }

}


    void Draft::printResult()
{
        cout << "Bond Price = " << setw(20) << bPrice << endl;
        cout << "Bond duration = " <<setw(20) << bDuration <<endl;
        cout << "Bond Convexity = " << setw(20) << bConvexity << "\n";
}


#include "Draft.h"
#include <iostream>
#include <cmath>
#include <array>
#include <iomanip>
using namespace std;


    int main (){

        double termArray[3]{1, 2, 3};
        double cFlowArray[3]{5, 5, 105};
        int arraySize = 3;



        Draft bond1 (arraySize, termArray, cFlowArray);


        Draft::Bcalculator();

        bond1.printResult();

        bond1.printDfactor();


        return 0;
}

The error is:错误是:

main.cpp|20|error: no matching function for call to 'Draft::Bcalculator include\Draft.h|18|note: candidate: 'void Draft::Bcalculator(int, double*, double*)'| main.cpp|20|错误:没有匹配的 function 调用'Draft::Bcalculator include\Draft.h|18|注意:候选:'void Draft::Bcalculator(int, double*, double*)'| include\Draft.h|18|note: candidate expects 3 arguments, 0 provided| include\Draft.h|18|注意:候选人期望 3 arguments,提供 0|

There are two problems in your code.您的代码中有两个问题。

  1. Definition does not match call.定义与调用不匹配。 You defined Bcalculator as:您将Bcalculator定义为:

    void Draft::Bcalculator(int sizeofArray, double term[], double cFlow[])

    But then you call it without arguments:但是你在没有 arguments 的情况下调用它:

    Draft::Bcalculator();

  2. To be able to call Draft::Bcalculator() you need to add static in the definition:为了能够调用Draft::Bcalculator() ,您需要在定义中添加static

    static void Draft::Bcalculator(int sizeofArray, double term[], double cFlow[])

    If you do not want to make it static , call it the normal way, ie Draft d{...}; d.Bcalculator()如果您不想将其static ,请按正常方式调用它,即Draft d{...}; d.Bcalculator() Draft d{...}; d.Bcalculator() . Draft d{...}; d.Bcalculator()

EDITED已编辑

I realized that Bcalculator is using the same three parameters you use to construct and store in Draft class. Therefore, you should call Bcalculator without any arguments and use the class members termArray , cFlowArray and arraySize :我意识到Bcalculator正在使用与您在Draft class 中构造和存储的相同的三个参数。因此,您应该在没有任何 arguments 的情况下调用Bcalculator并使用 class 成员termArraycFlowArrayarraySize

int main ()
{
    double termArray[3]{1, 2, 3};
    double cFlowArray[3]{5, 5, 105};
    int arraySize = 3;

    Draft bond1 (arraySize, termArray, cFlowArray);
    bond1.Bcalculator();
    bond1.printResult();
    bond1.printDfactor();
    return 0;
}

Then, your definition and implementation of this function has to be changed accordingly:然后,您必须相应地更改此 function 的定义和实现:

class Draft
{
public:
    ...
    void Bcalculator(); // <- remove parameters in this definition
private:
    double term[3];
    double cFlow[3];
    int sizeofArray;
}
void Draft::Bcalculator() // <- remove parameters in this implementation
{
    ... // use automatically the private members term, cFlow and sizeofArray
}

This code works, I compiled it.这段代码有效,我编译了它。

Regards!问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM