title: C++: Variable 'DiscreteGBM::S0' is uninitialized. Always initialize a member variable (type.6)
body: <p>I have an error message C++: Variable 'DiscreteGBM::S0' is uninitialized. Always initialize a member variable (type.6). This message is repeated a few times with names of different member variables.</p>
  
  <p>Can someone kind teach me how to initialize please? The header file cannot be changed. Only allowed changing of cpp file. However for cpp file, main function cannot be changed.</p>
  
  <p>This first code is for header file. This is not allowed to be changed</p>
  
  <pre><code>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&lt;double&gt; 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      
  };
  </code></pre>
  
  <p>CPP file below</p>
  
  <pre><code>#include "Functions.h"
  #include "PricingSimulation.h"
  
  
  void DiscreteGBM::SetRiskFreeRate(double r1) {
      r = r1;
  }
  
  void DiscreteGBM::SetInitialStockPrice(double s) {
      S0 = s;
  }
  
  void DiscreteGBM::SetVolatility(double s) {
      sigma = s;
  }
  
  void DiscreteGBM::SetTimeStepSize(double d) {
      delta = d;
  }
  
  void DiscreteGBM::SetNumberSteps(int n) {
      numberSteps = n;
  }
  
  vector&lt;double&gt; DiscreteGBM::PricePath() const {
      vector&lt;double&gt; stock;
      vector&lt;double&gt; rnorm;
      rnorm = randn(numberSteps);
      stock[0] = S0;
      for (int i = 1; i &lt;= numberSteps; i++) {
          stock[i] = stock[i - 1] * exp((r - 0.5 * sigma * sigma) * delta + sigma * sqrt(delta) * rnorm[i]);
      }
      return stock;
  }
  
  void EuropeanOption::SetCallPut(bool c) {
      callPut = c;
  }
  
  void EuropeanOption::SetStrike(double s) {
      strike = s;
  }
  
  double EuropeanOption::Payoff(double S_T) const {
      double value;
      if (callPut == 0)
          if ((S_T - strike) &gt; 0)
              value = S_T - strike;
          else
              value = 0;
      else
          if ((strike - S_T) &gt; 0)
              value = strike - S_T;
          else
              value = 0;
      return value;
  }
  
  double EuropeanOption::BlackScholesPrice() const {
      double value2, d1, d2;
      d1 = (1 / (sigma * sqrt(numberSteps * delta)) * (log(S0 / strike) + (r + sigma * sigma / 2) * numberSteps * delta));
      d2 = d1 - sigma * sqrt(numberSteps * delta);
      if (callPut == 0)
          value2 = normcdf(d1) * S0 - normcdf(d2) * strike * exp(-r * numberSteps * delta);
      else
          value2 = -normcdf(-d1) * S0 + normcdf(-d2) * strike * exp(-r * numberSteps * delta);
      return value2;
  }
  
  double EuropeanOption::MonteCarloPrice(int numberScenarios = 10000) const {
      int N = numberScenarios;
      double sum;
      double T = numberSteps * delta;
      vector &lt;double&gt; A;
      double O;
      for (int i = 1; i &lt;= N; i++) {
          A = PricePath();
          O = A[numberSteps];
          sum += O;
      }
      double average = sum / N;
      return exp(-r * T) * average;
  }
  
  int main()
  {
      SetSeed();
      EuropeanOption Test;
      for (double r = -0.1; r &lt;= 0.1; r += 0.05)
          for (double sigma = 0.1; sigma &lt;= 1; sigma += 0.3)
              for (int c = 0; c &lt;= 1; c++)
              {
                  Test.SetRiskFreeRate(r);
                  Test.SetInitialStockPrice(100);
                  Test.SetVolatility(sigma);
                  Test.SetNumberSteps(100);
                  Test.SetTimeStepSize(0.1);
                  Test.SetStrike(130);
                  Test.SetCallPut(c);
                  cout &lt;&lt; "r = " &lt;&lt; r &lt;&lt; ", sigma = " &lt;&lt; sigma;
                  cout &lt;&lt; ", c = " &lt;&lt; c &lt;&lt; ": ";
                  cout &lt;&lt; Test.BlackScholesPrice() &lt;&lt; " , ";
                  cout &lt;&lt; Test.MonteCarloPrice() &lt;&lt; endl;
              }
  
      system("pause");
  }
  </code></pre>