mercredi 22 avril 2015

Linker refers to, supposedly, undefined reference to vtable

Last time I asked you about inheritance, it turns out I'll be forced to do so yet again.

This time I am trying to use an abstract class to represent a common ground for subtypes. However, it(the linker it seems) keeps moaning about vtables and undefined references no matter what I do. Judging by the errormessages, the problem must be related to the destructors in some way. Wierdldy enough, it keeps talking about a "undefined reference to 'AbstractBase::~AbstractBase()'" IN child.cpp. Which makes no sense.

Like last time, I can't actually show my code, so here is an example that in essence does the same thing:

first the abstract class, "AbstractBase.h":

#ifndef ABSTRACTBASE
#define ABSTRACTBASE

class AbstractBase
{
   public:
   virtual ~AbstractBase() = 0;
}
#endif

The child that uses the abstractbase, "child.h":

#ifndef CHILD
#define CHILD

class child : public AbstractBase
{
   public: 
      ~child() override;
}
#endif

The implementation in "child.cpp":

#include "child.h"
child::~child()

Obviously there are far more functions, but in essence that's how my real class's destructors look.

After scouring the web for ways of using abstract classes in C++, I am about to give up. As far as I can tell from those sources, this is the way to do it. You declare your abstracts class's destructor virtual, so any call to it will include the child. And the childs destructor is simply marked override. There shouldn't be anything else to it.

Have I missed something truly fundamental here?

Aucun commentaire:

Enregistrer un commentaire