简体   繁体   中英

Undefined reference to vtable in Qt

I am getting a virtual table error while I can not detect any faults in my code. Could someone give me a point in the right direction? Sorry for the dutch terminology, hopefully it isn't an issue.

#ifndef LIJST_H
#define LIJST_H

#include "product.h"
#include <list>

typedef list<Product*> lijst;

#endif // LIJST_H

// Methode

#ifndef METHODE_H
#define METHODE_H

#include "lijst.h"

class Methode
{
public:
    Methode() {}
    virtual ~Methode() {}
    virtual double run(lijst *items);
};

#endif // METHODE_H

// Productmethode
#ifndef PRODUCTMETHODE_H
#define PRODUCTMETHODE_H

#include "methode.h"

class ProductMethode : public Methode
{
private:
    map<string,double> kortingsTabel;
public:
    ProductMethode() {}
    void addKorting(string naam, double korting);
    double run(lijst *items);
};

// Main
#include <QCoreApplication>
#include "factuur.h"
#include "product.h"
#include "globalemethode.h"
#include "productmethode.h"
#include "drempelmethode.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    GlobaleMethode G ( 0.1 );
    ProductMethode P;
    P.addKorting ( "Melk", 0.1 );
    P.addKorting ( "Boter", 0.05 );
    Factuur F ( &G );
    F.addProduct ( new Product ( "Melk", 0.75 ) );
    F.addProduct ( new Product ( "Kaas", 5 ) );
    F.addProduct ( new Product ( "Boter", 1.7 ) );
    cout << F.totaal ( ) << endl;
    F.setMethode ( &P );
    cout << F.totaal ( );

    return a.exec();
}

You just declared a method in class Methode :

virtual double run(lijst *items);

The method needs to be defined as well. Only pure virtual functions are allowed to be without a definition.

Also, one would usually use virtual to overidde behavior of certain base class method for derived class, but in your example you do not provide a definition for the overidden virtual method. You should. If you don't need to then why did you make the method virtual to begin with? It need not be virtual .


Good Read:

What does it mean that the "virtual table" is an unresolved external?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM