【问题标题】:template inheritance and abstract class模板继承和抽象类
【发布时间】:2014-08-12 20:14:55
【问题描述】:

我对以下课程有 2 个问题。我有两个错误:第一个可能是模板类之间的继承问题,另一个是当这个类实际上不是抽象时初始化一个抽象类(参见代码中的 cmets)

some_header.h

 template <typename T, typename R>
 class SDE  // abstract class
 {

     protected:
          T drift, diffusion;  // drift and diffusion terms
          T initialValue;
          Interval<R> range;


     public:
        virtual  T GetInitialValue() = 0;  // initial condition
        virtual  T GetDrift() = 0;
        virtual  T GetDiffusion() = 0;
        virtual  ~SDE();


};

#include "SDE.h"
#include <cmath>


 template <typename T, typename R>    // Cox, Ingersoll, Ross sde
 class CIRSDE : public SDE<T,R>
 {


  private:
       T kappa, theta, sigma;

  public:
        CIRSDE(const T& _initialValue,
               const Interval<R>& _range,
               const T& _kappa,
               const T& _theta,
               const T& _sigma);

        CIRSDE();
        ~CIRSDE(){};

        T GetInitialValue();
        T GetDrift(const T& t, const T& r);
        T GetDiffusion(const T& t, const T& r);

};


template <typename T, typename R>
CIRSDE<T,R> :: CIRSDE(const T& _initialValue,
                  const Interval<R>& _range,
                  const T& _kappa,
                  const T& _theta,
                  const T& _sigma)
{
   kappa = _kappa;
   theta = _theta;
   sigma = _sigma;
   SDE<T,R> :: initialValue = _initialValue;
   SDE<T,R> :: range = _range;
}

template <typename T, typename R>
CIRSDE<T,R> :: CIRSDE()
{
   kappa = 1;
   theta = 1;
   sigma = 1;
   SDE<T,R> :: initialValue = 1;
   SDE<T,R> :: range = Interval<R>(0,1);
}


template <typename T, typename R>
T CIRSDE<T,R> :: GetDrift (const T& t, const T& r)
{
     return kappa * (theta - r);
}

 template <typename T, typename R>
 T CIRSDE<T,R> ::  GetDiffusion(const T& t, const T& r)
 {
     return sigma * sqrt(r);
 }

 template <typename T, typename R>
 T CIRSDE<T,R> :: GetInitialValue()
 {
       return  initialValue;     // ERROR 1
       // undeclared identifier "initialValue"
 }

main.cpp

 #include "some_header.h"

 int main()
 {
     Interval<int> range(0,5);

     CIRSDE<int, int> a (1, range, 3,3,3); //ERROR2
     // variable CIRSDE<> is an abstract class

     return 0;
 }

【问题讨论】:

  • 第一个错误是依赖名称查找的问题(使用非限定名称时不搜索依赖模板参数的基类)。就像在构造函数中一样使用SDE&lt;T,R&gt; :: initialValue。
  • 第二个错误可能是你似乎试图覆盖CIRSDE中的虚函数GetDiffusion和GetInitialValue。重写虚函数需要使用完全相同的参数列表和协变返回类型。 IE。您不是覆盖这些函数,而是使用 新的、不相关的 函数隐藏基类函数。
  • 下次请提供一个完整的例子。这里缺少类模板Interval。
  • 嗯。我试图了解您如何将其分成文件,但这对我来说不太有意义。随意解决这个问题。请注意,模板必须在头文件中定义(除非您明确实例化或专门化它们)。

标签: c++ templates inheritance abstract-class


【解决方案1】:

错误 1:

 template <typename T, typename R>
 T CIRSDE<T,R> :: GetInitialValue()
 {
       return  initialValue;     // ERROR 1
       // undeclared identifier "initialValue"
 }

这是查找的问题。标识符initialValue 不依赖于模板参数,因此在替换实际类型之前的第一次传递期间被解析,并且不检查基数(在您替换类型之前,基数实际上是未知的!)

您可以像之前的SDE&lt;T,R&gt;::initialValue 一样通过限定或使用this 来解决它:

       return this->initialValue;

错误 2

CIRSDE<int, int> a (1, range, 3,3,3); //ERROR2
// variable CIRSDE<> is an abstract class

问题是基础有几个纯虚函数,您没有在CIRSDE 中提供定义。特别是:

    virtual  T GetDrift() = 0;
    virtual  T GetDiffusion() = 0;

请注意,派生类型中的以下内容不会被覆盖,因为它们的签名不匹配:

    T GetDrift(const T& t, const T& r);
    T GetDiffusion(const T& t, const T& r);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多