I am trying to set up class hierarchy as follows:
template< class TProcess>
class Path
{
protected:
TProcess &process;
double Xt;
public:
Path(TProcess &process, double X0) : process(process), Xt(X0) {}
virtual ~Path(){}
virtual double dX(double dt, const std::vector<double> &dW) = 0;
};
template< class TProcess>
class Process
{
protected:
double X0;
public:
Process(double X0) : X0(X0) {}
virtual ~Process(){}
virtual boost::shared_ptr<Path<TProcess> > NewPath() =0;
};
but then I found it impossible to declare subclasses so that it works. I tried:
class GeometricBrownianMotion;
class GBMPath: public Path<GeometricBrownianMotion>
{
double dX(double dt, const std::vector<double> &dW)
{
Xt = Xt*exp(process.mu*dt - 0.5*dt + process.sigma*dW);
return Xt;
}
};
class GeometricBrownianMotion: public Process<GeometricBrownianMotion>
{
double mu;
const std::vector<double> σ
double sigma2;
public:
GeometricBrownianMotion(double X0, double mu, const std::vector<double> &sigma): Process(X0), mu(mu), sigma(sigma), sigma2(sigma*sigma)
{}
virtual boost::shared_ptr<Path<GeometricBrownianMotion> > NewPath()
{
return boost::shared_ptr<Path<GeometricBrownianMotion> >(new GBMPath(*this, X0));
}
};
but I get the following error:
process.h:58: error: forward declaration of ‘struct GeometricBrownianMotion’
process.h:63: error: invalid use of incomplete type ‘struct GeometricBrownianMotion’
Any ideas how to make it work?
Just separate your implementation from your class definitions, and the problem will go away nearly automatically.
Nearly, because you might just want to move the definition of
GeometricBrownianMotionbefore using it as a template parameter.EDIT: even not nearly, at least on my compiler, removing all inline methods definitions was just enough to get it to compile.