2013-03-27 102 views
0

我收到虛擬表錯誤,但無法檢測到代碼中的任何錯誤。有人能給我一個正確的方向嗎?對不起,荷蘭語術語,希望它不是一個問題。未定義的引用到Qt中的vtable

#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(); 
} 

回答

4

你剛纔宣佈Methode類中的方法:

virtual double run(lijst *items); 

的方法需要被定義好。只有純粹的虛擬功能可以沒有定義。

此外,人們通常會使用virtual來描述派生類的某些基類方法的行爲,但在您的示例中,您沒有提供對過濾方法的定義。你應該。如果你不需要那麼你爲什麼要開始使用方法virtual?它不一定是virtual


讀取良好:

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

+0

我打算爲純虛函數,這顯然需要被定義爲 虛擬雙運行(lijst *項目)= 0; 。另外,我確定了ProductMethode中的run方法,但是我忘記了顯示cpp內容。謝謝您的幫助! :) – RobotRock 2013-03-27 11:03:33

相關問題