0
我試圖建立的類層次結構如下:模板和繼承在C++
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;
};
,但後來我發現,這樣它的作品無法申報的子類。我想:
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));
}
};
,但我得到了以下錯誤:
process.h:58: error: forward declaration of ‘struct GeometricBrownianMotion’
process.h:63: error: invalid use of incomplete type ‘struct GeometricBrownianMotion’
任何想法如何使它工作嗎?
從'GeometricBrownianMotion'和'GBMPath'中刪除內聯方法定義? – Grzenio 2011-12-18 22:11:11