0

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;
}
H.Nam
  • 19
  • 4
  • 2
    It seems like you have several misconceptions about how C++ deals with memory, and should perhaps open a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)--there are a number of different errors here, and a few more instances of plain bad practice, that would likely be resolved by reading a good book on the matter. – jaggedSpire Nov 12 '16 at 00:24

1 Answers1

0

Your TrainerClass has a pure virtual (=0) function AddItems(). This prevents you from instantiating any object of this class. Unfortunately you try to do this in your for-loop, and this causes the error.

Note by the way that if it would work, it would be undefined behavior, because you've never initialized the pointer stuff (you only assign stuff elements).

Here a variant which works and which use #include <vector> for more convenience:

...
vector<TrainerClass*> stuff(nbrtrainer);  // vector of the given dynamic size
//for(int itrainer=0;itrainer<nbrtrainer;itrainer++) 
//   stuff[itrainer] = new TrainerClass;  // you can't instantiate an abstract class 
stuff[0] = new BagClass(0.2);  // this is a valid instantiation
stuff[1] = new BagClass(0.5);
// 
...

Here the online demo.

And here an even more dynamic version:

vector<TrainerClass*> stuff;  // empty vector
stuff.push_back(new BagClass(0.2));  // just add a new eleemnt at the end of the vector
stuff.push_back(new BagClass(0.5));

for (int i=0; i<stuff.size(); i++) 
   stuff[i]->AddItems();

The next improvement would be to use a smart pointer instead of a raw pointer ;-)

Christophe
  • 68,716
  • 7
  • 72
  • 138