2

I'm creating a simple abstract class named Asset, the header code looks like this:

#ifndef ASSET_H
#define ASSET_H

#include <QString>
#include <QDate>

class Asset
{  
public:
    Asset(QString des, QDate dat);
    ~Asset();
    virtual QString toString();
    virtual double value();
    QString getDescription();

private:
    QString description;

protected:
    QDate date;
};

#endif // ASSET_H

This is what my implementation file looks like:

#include "asset.h"

Asset::Asset(QString des, QDate dat)
{
    description = des;
    date = dat;
}

QString Asset::getDescription()
{
    return description;
}

The error code I'm getting on the implementation constructor is: undefined reference to vtable for Asset

What am I doing wrong here?

Marco A.
  • 43,032
  • 26
  • 132
  • 246
Siya Khumalo
  • 143
  • 1
  • 10

1 Answers1

5

The methods you do not implement in the abstract class, should be pure virtual methods. That mean they should be followed by = 0.

For example:

virtual double value() = 0;

This tells the compiler that this is a pure virtual method. For an explanation of the difference between virtual methods and pure virtual methods, take a look at this Stackoverflow answer: C++ Virtual/Pure Virtual Explained

On a side note. When you have an abstract class, you should really make sure that your destructor is virtual. Always use virtual destructors in C++ when you deal with class that you intend to be inherited from.

In your class design you have a private member called description, and a public function to get the description that is not virtual. You have no methods to set the description, perhaps you should consider moving the description member variable from private to protected or perhaps even creating a mutator function to set it.

Community
  • 1
  • 1
Tommy Andersen
  • 7,165
  • 1
  • 31
  • 50