When compiling the c++ code below, I'm getting the following error message:
"Invalid new-expression of abstract class type 'TrainerClass' because the function 'virtual void TrainerClass::AddItems()' is pure within 'TrainerClass'".
The purpose of this code is to initialise a number of "stuff" objects equivalent to the number of trainer (int nbrtrainer = 2;). In this example, 2 "stuff" objects should be created: stuff[0] and stuff[1] (pointers to the 'TrainerClass' class). Then, what's inside each "stuff" object should be defined by pointing the 'BagClass' class to the "stuff" objects.
The difficulty here is that the 'TrainerClass' class includes a pure virtual function which prevents to initialise the class objects as usual.
What is the correct syntax to initialise objects derived from a virtual class using the dynamic binding concept? (All objects should be called 'stuff[x]' with an index)
#include <iostream>
using namespace std;
#define Training // to declare the global variable
///Definition of class Pocket
class pocket
{
public:
double volume;
};
///Definition of virtual class TrainerClass
class TrainerClass
{
public:
pocket *Space;
virtual void AddItems()=0;
};
///Definition of derived class
class BagClass: public TrainerClass
{
public:
double mass;
BagClass(double m2)
{
mass=m2;
}
void AddItems()
{
cout << "out virtual " << endl;
}
};
#ifdef Training
#define Training
TrainerClass **stuff=0;
#else
extern TrainerClass **stuff;
#endif
int main()
{
int nbrtrainer = 2;
TrainerClass **stuff;
for(int itrainer=0;itrainer<nbrtrainer;itrainer++) stuff[itrainer] = new TrainerClass;
stuff[0] = new BagClass(0.2);
stuff[1] = new BagClass(0.5);
stuff[0]->AddItems();
return 0;
}