cannot instantiate abstract class because members

2019-08-08 22:57发布

I have this abstract base class ODESolver:

//ODESolver.h
#ifndef ODESolver_H
#define ODESolver_H
#include "doublependulum.h"
class ODESolver
{
public:
    ODESolver( DoublePendulum&);           //constr
    virtual ~ODESolver();                  //destr
    virtual void predict (const double &)=0; //virtual
protected:
    DoublePendulum DoublePend;
};
#endif

with implementation:

//ODESolver.cpp
#include "ODESolver.h"
//constructor
ODESolver::ODESolver( DoublePendulum & param)
          :DoublePend(param)
{}
//destructor
ODESolver::~ODESolver(){}

I also have this class ODEEuler wich inherits from ODESolver

//ODEEuler.h
#ifndef ODEEuler_H
#define ODEEuler_H
#include "ODESolver.h"
class ODEEuler : public ODESolver
{
public:
    ODEEuler(DoublePendulum &);
    virtual ~ODEEuler();     
    virtual void Predict(const double &);
};
#endif

with implementation

//ODEEuler.cpp
#include "ODEEuler.h"
#include <iostream>
using namespace::std;
ODEEuler::ODEEuler (DoublePendulum &param) 
         :ODESolver(param)
{}
//destructor
ODEEuler::~ODEEuler(){}
 void ODEEuler::Predict(const double &dt=0.01)
{
    DoublePend=DoublePend+DoublePend.Derivative()*dt;
    cout << DoublePend.getUp().getTheta() << endl; \\ I want to print getTheta on the screen form my getUp Pendulum from my Doublepend
}

I now want to test my ODEEuler, so I made an object in my Main file:

//Main.cpp
#include "pendulum.h"
#include "doublependulum.h"
#include "ODEEuler.h"
#include "ODESolver.h"
#include <iostream>
using namespace::std;
int main()
{ 
Pendulum MyPendulum(1.5,1.5,1.5,1.5);
DoublePendulum MyDoublePendulum(MyPendulum,MyPendulum,9.81);  
ODEEuler myODEEuler(MyDoublePendulum);
return 0;
}

I keep getting this error:

1>....\main.cpp(24): error C2259: 'ODEEuler' : cannot instantiate abstract class 1> due to following members: 1> 'void ODESolver::predict(const double &)' : is abstract 1>
.....\odesolver.h(11) : see declaration of 'ODESolver::predict'

I checked if all the types that I used in my virtual void predict function are the same as elsewhere. I guess it is maybe something conceptual I do wrong. What exactly does it mean that I cannont instantiate ,because 'predict' is abstract?

Thanks in advance for your support!

标签: c++
1条回答
疯言疯语
2楼-- · 2019-08-08 23:33

Your derived class needs to implement the pure virtual function to be able to be instantiable.

virtual void predict (const double &)=0;
             ^

void ODEEuler::Predict(const double &dt=0.01)
               ^

Notice the capital p it makes the method in derived class a different method and you end up never defining the Base class pure virtual method.

查看更多
登录 后发表回答