Domanda

I was trying to make a nested strategy pattern. I get an error as when making the nested parent class purely virtual. Is this idea even possible?

Example:

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);

};

Resulst in the following error:

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()

The implementations will be in the cpp file of cause.

È stato utile?

Soluzione

You need to pass the parameter by reference (or pointer) instead of by value.

Jacobi(Jacobi::DiagAlg const& DA);

(Also, on an unrelated note, do not use identifiers beginning with an underscore and a capital letter. Those are reserved for the implementation.)

Altri suggerimenti

By rules you can't create instance of an abstract class. Your passing by value forcing that. Add & or const& to fix.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top