【发布时间】: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++