我收到一条错误消息C ++:变量'DiscreteGBM :: S0'未初始化。始终初始化成员变量(类型6)。使用不同的成员变量的名称重复此消息几次。
有人可以教我如何初始化吗?头文件无法更改。仅允许更改cpp文件。但是对于cpp文件,主要功能无法更改。
第一个代码用于头文件。不允许更改
toDate()
下面的CPP文件
class DiscreteGBM
{
protected:
double r; // risk free interest rate
double S0; // stock price at time 0
double sigma; // volatility
double delta; // length of time steps
int numberSteps; // total number of time steps;
// start time is 0, end time is numberSteps*delta
public:
DiscreteGBM() {}
void SetRiskFreeRate(double r1); // set r to r1
void SetInitialStockPrice(double s); // set S0 to s
void SetVolatility(double s); // set sigma to s
void SetTimeStepSize(double d); // set delta to d
void SetNumberSteps(int n); // set numberSteps to n
vector<double> PricePath() const; // compute price path S_0,...,S_T (where T=delta*n)
// according to formula (1) in the instructions,
// and return the vector (S_0,...,S_T)
};
// class for simulation of European call and put options
class EuropeanOption : public DiscreteGBM
{
protected:
bool callPut; // callPut =0 if object is call option, callPut=1 if it's a put option
double strike; // strike price of option
public:
EuropeanOption() {}
void SetCallPut(bool c); // set callPut to c
void SetStrike(double s); // set strike to s
double Payoff(double S_T) const; // return payoff of option according to
// formulas (2) and (3) of the instructions;
// S_T is the stock price at maturity
double BlackScholesPrice() const; // return Black Scholes price of the option
// according to formulas (4) and (6) of the instructions
double MonteCarloPrice(int numberScenarios=10000) const;
// execute algorithm Monte Carlo pricing given
// in instructions
};