2015-04-04 71 views
0

我已經看到所有其他的答案這個錯誤,但我試過所有我能找到,仍然沒有工作。未定義引用'派生類的vtable'

這裏是我的代碼:

class Train{ 
    protected: 
     string myID; 
     int myCap; 
     int myPass; 
     char myType; 
    public: 
     Train(string str,int num): myID(str), myCap(num){myPass=0;} 
     virtual ~Train(){}; 
     string getmyID(){return myID;} 
     int getmyCap(){return myCap;} 
     char getmyType(){return myType;} 
     virtual void showTrainInfo()=0; 
     virtual int getCost(string,string)=0; 

     virtual bool operator== (const string)=0; 
}; 



class STrain: public Train{ 
    public: 
     STrain(string str, int num): Train(str,num){myType='S';} 
     ~STrain(){cout<<"Deleted Train:"<<myID<<endl;} 
     void showTrainInfo(){ 
      cout << "Train ID: " << this->myID << endl; 
      cout << "Train Capacity: " << this->myCap << endl; 
      cout << "Train Type: " << this->myType << endl; 
     } 
     int getCost(string origin, string dest); 
     bool operator== (const string a){ 
     bool compare = false; 
      if(myID.compare(a) == 0) 
       compare = true; 

     return compare; 
     } 
}; 

的錯誤是在派生功能應變。我也嘗試在基類Train中定義我的虛擬析構函數,但它仍然給出了同樣的錯誤。

我看過的其他錯誤,Train中的虛函數沒有定義,並且通過將{}添加到虛函數來解決,但正如您所看到的,它不適用於我。任何建議或解釋爲什麼會發生這種情況?

+2

您是否在某個地方定義了'STrain :: getCost'? – 2015-04-04 04:41:16

+0

尚未盡管我嘗試評論所有'int getCost'出來,它並沒有解決任何問題。我定義了'STrain :: getCost',它看起來好像處理了它。謝謝 – 2015-04-04 09:12:32

回答

1

加入一些傢俱重現你的錯誤後,我可以把它通過定義STrain::getCost功能消失:

int STrain::getCost(string origin, string dest) { return 0; } 

然後它編譯成功。

如果您仍然遇到問題,請發佈MCVE,否則我們只是猜測您在其他代碼中的含義。

+0

你是對的,錯誤確實消失了。你碰巧知道爲什麼?我試圖一起刪除功能,但它沒有工作,所以我以爲'getCost'不是導致錯誤的那個。謝謝您的幫助! – 2015-04-04 09:14:03

+0

@ChelseaTalay是的,因爲它是基類中的純虛函數,所以您必須在任何派生類中爲其創建實例(即使您不調用該函數)爲其提供實體。該錯誤消息有點神祕。 – 2015-04-04 10:03:54