C++ 函数重载问题

kit*_*its 0 c++ overloading vector

我正在运行一个期权定价模型,该模型为四种不同的期权生成四个值。

class EuroOption
{
private:
    double S;    //underlying stock price
    double X;    //strike price
    double sigma;    //volatility
    double T;    //time to expiration
    double r;    //risk-free rate
    double b;    //cost of carry
public:
    EuroOption();    //default constructor
    ~EuroOption();    //destructor
    EuroOption(const EuroOption& eo);    //copy constructor
    EuroOption& operator = (const EuroOption& source);    //assignment operator
    EuroOption(vector<double> Batch1);
    EuroOption(vector<double> Batch2);  //this is the error: redeclaration
    //EuroOption(vector<double> const Batch3);
    //EuroOption(vector<double> const Batch4);
Run Code Online (Sandbox Code Playgroud)

以下是 .cpp 的源材料:

EuroOption::EuroOption(vector<double> Batch1) : S(60), X(65), sigma(0.30), r(0.08), T(0.25), b(r)
{
}

EuroOption::EuroOption(vector<double> Batch2) : S(100), X(100), sigma(0.20), r(0), T(1), b(r)
{
}
Run Code Online (Sandbox Code Playgroud)

我收到的错误是“构造函数无法重新声明”。但我的函数有不同的参数(Batch1/Batch2),所以我不明白为什么它没有重载。Batch2 的输出也与 Batch 1 相同(这是不正确的)。我将不胜感激您的指导。

cpp*_*der 5

重载基于参数类型而不是参数名称。

EuroOption::EuroOption(vector<double> Batch1)  
Run Code Online (Sandbox Code Playgroud)

这里vector<double>是参数类型,Batch1是参数名称。
如果你想要重载函数,你应该声明具有不同参数类型或不同参数数量的函数。

例如,这些是重载函数,

EuroOption::EuroOption(vector<double> Batch1)
EuroOption::EuroOption(vector<int> Batch1)
EuroOption::EuroOption(string Batch1)
Run Code Online (Sandbox Code Playgroud)