0
votes

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. The program uses two arrays and a integer to do calculation. I used a loop in constructor to initialize data members (learned from 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. 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*)'| include\Draft.h|18|note: candidate expects 3 arguments, 0 provided|

1
What is the complete error you're receiving? I assume it has something to do with Draft::Bcalculator();. It isn't a static function so you need to call it with an instance of the class, like bond1.Bcalculator(); as you did with the other functions right below it. - Retired Ninja
A useful way to learn C++ is to take a situation like yours and concoct a minimal reproducible example demonstrating the error. Forget your goal of calculating a price and focus on the syntax related to this error. Figure out what is relevant, and get rid of the rest (in a copy of your project, of course). As long as the first error message does not change (other than the line and character numbers), you are on track for a simpler example. - JaMiT
main.cpp|20|error: no matching function for call to 'Draft::Bcalculator include\Draft.h|18|note: candidate: 'void Draft::Bcalculator(int, double*, double*)'| include\Draft.h|18|note: candidate expects 3 arguments, 0 provided| - Puckpicker
Sorry, errors looked messy. I have one error now. it looks like the data can't be passed to member function to do calculation - Puckpicker
Ok with that error (that I moved into the question) the compiler tells you on line 20 of main you call Draft::Bcalculator(); with 0 parameters but it requires 3. The compiler is correct on this. - drescherjm

1 Answers

0
votes

There are two problems in your code.

  1. Definition does not match call. You defined Bcalculator as:

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

    But then you call it without arguments:

    Draft::Bcalculator();

  2. To be able to call Draft::Bcalculator() you need to add static in the definition:

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

    If you do not want to make it static, call it the normal way, i.e. 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:

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:

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!