-4

I have a few issues with my the following classes . When I create an object of type "MonteCarloSingleAsset" and I apply to it the method "GetPrice()", I get this:

copy\pricinglib\montecarlosingleasset.h(81): error C2597: illegal reference to non-static member 'MonteCarlo::stepsNumber'

copy\pricinglib\montecarlosingleasset.h(85): error C2597: illegal reference to non-static member 'MonteCarlo::N'

copy\pricinglib\montecarlosingleasset.h(85): error C3867: 'MonteCarlo::N': function call missing argument list; use '&MonteCarlo::N' to create a pointer to member

Thanks in advance for your help.

In MonteCarlo.h

#include <vector>
#include <cmath>
#include <string>



using namespace std;

template<typename T>
class MonteCarlo
{
   public:
        virtual ~MonteCarlo(){};

        MonteCarlo(unsigned long& _N,   unsigned long& _stepsNumber );

   protected:     
          T GetDiscountFactor(const double& r, const double& T);

          unsigned long N;  // num of sim
          unsigned long stepsNumber;  // steps for sde discretisation
        ;
 };

 template<typename T>
 T MonteCarlo<T> :: GetDiscountFactor(const double& r, const double& T) 
 {
    return exp(-r*T);
 }

 template<typename T>
 MonteCarlo<T> :: MonteCarlo(unsigned long& _N,   unsigned long& _stepsNumber)
 {
     N = _N;
     stepsNumber = _stepsNumber;
 }

In MonteCarloSingleAsset.h

#include "MonteCarlo.h"


template<typename T, char R>   
class MonteCarloSingleAsset
{
      public:


        T GetPrice(SingleAssetOption& option);

        MonteCarloSingleAsset(const unsigned long& _N, 
                             const string& SDEType,
                              const string& SDESimulType,
                               const unsigned long& _stepsNumber = 25);

        virtual ~MonteCarloSingleAsset(){};

      protected:
           string SDEType, SDESimulType; 


};

template<typename T, char R>
MonteCarloSingleAsset<T,R> :: MonteCarloSingleAsset(const unsigned long& _N, 
                                         //  const string& _randType, 
                                                const string& _SDEType,
                                                const string& _SDESimulType,
                                                const unsigned long& _stepsNumber)      

  :  MonteCarlo<T>(_N, _stepsNumber)

  {
     SDEType = _SDEType;  // GBM, VAS, MER, CIR
     SDESimulType = _SDESimulType; // EUMAR, MIL = milstein
  }


template<typename T, char R>
T MonteCarloSingleAsset<T,R> ::  GetPrice(SingleAssetOption& option)
{


    T sum = 0.0;
    T payOff = 0.0;
    vector<T> path;
    vector<T> rand;
    Interval<T> range(0.0, option.GetT());

    BasicMethod<T> randGen(1);

    GBMSDE<T> sde(option.GetS(), range, option.GetR() , option.GetVol());



    EulerMaruyamaScheme simulator(MonteCarlo<T> :: stepsNumber);


     for (unsigned long i = 1; i <= MonteCarlo<T> :: N ; i++)
     {
        rand = randGen.GetRandomVector( MonteCarlo<T> :: stepsNumber);
        path = simulator.SimPath(sde,rand);
        payOff = option.GetPayOff(path); 
        sum += payOff;
     }    

      double disFact = MonteCarlo<T> :: GetDiscountFactor(option.GetR(), option.GetT());

      return (1 / MonteCarlo<T> :: N )*disFact*sum; 
 }
2
  • 3
    For starters, where is the inheritance? Jun 30, 2014 at 23:01
  • To access MonteCarlo<T>::stepsNumber the stepsNumber must be defined as static ( static unsigned long stepsNumber)
    – AdamF
    Jun 30, 2014 at 23:02

1 Answer 1

0

The error "illegal reference to non-static member" tells you that you're trying to access a member variable or function as if it was static.

For example, with MonteCarlo<T> :: stepsNumber the MonteCarlo<T> part is just the name of a type, not an actual instance of it.

Either you need to change your MonteCarlo class to have static members.

Or else you need to create instances. For example:

MonteCarlo<T> m;
m.stepsNumber;

Or perhaps you meant for MonteCarloSingleAsset to derive from MonteCarlo, which would change the situation significantly. However that's not what your code is currently doing.

Not the answer you're looking for? Browse other questions tagged or ask your own question.