在Qt中对vtable的未定义引用 [英] 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();
}
推荐答案
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.
此外,通常使用 virtual
重载派生类的某些基类方法的行为,示例您不为覆盖的 virtual
方法提供定义。你应该。如果你不需要那么为什么你使方法 virtual
开始?不需要 virtual
。
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
.
:
What does it mean that the "virtual table" is an unresolved external?
这篇关于在Qt中对vtable的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!