2015-04-04 83 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

相关问题