0

我试图制作一个嵌套的策略模式。在将嵌套父类设为纯虚拟时出现错误。这个想法甚至可能吗?

例子:

class Jacobi {
 private:
  mat _V, _A;
  int _n, _rotations;

 public:
  class DiagAlg {
  public:
  virtual void diagonalize() = 0;
  };
  class Cyclic : DiagAlg {
  public:
    void diagonalize();
  };

  vec getE();
  mat getV();
  mat getA();
  int getRotations();
  Jacobi(Jacobi::DiagAlg DA);
  Jacobi(const mat& A); // could be done without user supply of base vectors
  bool rotate(int p, int q);

};

导致以下错误:

jacobi.h:28:26: error: cannot declare parameter ‘DA’ to be of abstract type ‘Jacobi::DiagAlg’
jacobi.h:15:9: note:   because the following virtual functions are pure within ‘Jacobi::DiagAlg’:
jacobi.h:17:16: note:   virtual void Jacobi::DiagAlg::diagonalize()

实现将在原因的 cpp 文件中。

4

2 回答 2

2

您需要通过引用(或指针)而不是按值传递参数。

Jacobi(Jacobi::DiagAlg const& DA);

(另外,在不相关的说明中,不要使用以下划线和大写字母开头的标识符。这些是为实现保留的。)

于 2013-06-07T17:04:20.693 回答
1

根据规则,您不能创建抽象类的实例。你的价值传递迫使这一点。添加 & 或 const& 来修复。

于 2013-06-07T17:03:07.113 回答