【问题标题】:No matching function for call to a member function in class调用类中的成员函数没有匹配的函数
【发布时间】:2020-12-07 02:30:30
【问题描述】:

我是 C++ 新手,正在尝试制作一个计算债券价格的程序,原始代码运行良好,但我很难将其转移到 OOP。模式。该程序使用两个数组和一个整数进行计算。我在构造函数中使用了一个循环来初始化数据成员(从堆栈溢出中学习)。它看起来不错,但我遇到了一个错误,例如:没有匹配函数调用成员函数。数据不能传递给成员函数。我被困在这里一整天。有人可以给我一些见解吗?谢谢你。代码如下:

#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;
}

错误是:

main.cpp|20|错误:没有匹配的调用函数 'Draft::Bcalculator 包括\Draft.h|18|注:候选:'void 草稿::Bcalculator(int, double*, double*)'|包括\Draft.h|18|注意: 候选人需要 3 个参数,提供 0 个|

【问题讨论】:

  • 您收到的完整错误是什么?我认为它与Draft::Bcalculator(); 有关。它不是静态函数,因此您需要使用类的实例来调用它,例如 bond1.Bcalculator();,就像使用它下面的其他函数一样。
  • 学习 C++ 的一个有用方法是考虑像你这样的情况并编造一个 minimal reproducible example 来演示错误。忘记计算价格的目标,并专注于与此错误相关的语法。弄清楚什么是相关的,然后摆脱其余的(当然是在你的项目的副本中)。只要第一个错误消息没有改变(除了行号和字符号),您就可以找到一个更简单的示例。
  • main.cpp|20|error: no matching function for call to 'Draft::Bcalculator include\Draft.h|18|note: Candidate: 'void Draft::Bcalculator(int, double* , 双*)'| include\Draft.h|18|注意:候选人需要 3 个参数,提供 0 个|
  • 抱歉,错误看起来很乱。我现在有一个错误。看起来数据无法传递给成员函数进行计算
  • 好吧,那个错误(我进入了这个问题)编译器在 main 的第 20 行告诉你,你用 0 个参数调用 Draft::Bcalculator();,但它需要 3 个。编译器对此是正确的。

标签: c++


【解决方案1】:

你的代码有两个问题。

  1. 定义与调用不匹配。您将Bcalculator 定义为:

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

    但是你不带参数地调用它:

    Draft::Bcalculator();

  2. 为了能够调用Draft::Bcalculator(),您需要在定义中添加static

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

    如果你不想让它成为static,就用正常的方式调用它,即Draft d{...}; d.Bcalculator()

已编辑

我意识到Bcalculator 正在使用与您在Draft 类中构造和存储相同的三个参数。因此,您应该不带任何参数调用Bcalculator,并使用类成员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;
}

那么,你对这个函数的定义和实现必须相应地改变:

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
}

这段代码有效,我编译了它。

问候!

【讨论】:

  • @Victor,我定义了 Bcalculator 静态,然后我不得不在函数静态中声明变量,以更多错误结束。我想我错过了其他东西。
  • 嗨,维克多,谢谢。调用 Draft::Bcalculator() 之类的函数是错误的。它不是静态函数,应该以正常方式调用。我修复了它,但仍然给了我同样的错误。我会继续调整。
  • 只要函数使用成员变量,就不能将其转换为静态(除非这些成员也是静态的),这就是您看到错误的原因。调用Bcalculator 首先创建一个Draft 对象,然后调用它的方法,这必须有效。
  • 我做了一个项目bond1,然后像下面这样调用,但仍然出现同样的错误。
  • 你好维克多,非常感谢你^n。你保存了我的程序。一个小问题,函数 printDfactor 不能正常工作,它打印出 disF[] 但数字太小(接近 0)。再次感谢。
猜你喜欢
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
  • 2020-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多