Undefined reference to vtable in final class constructor and destructor
本问题已经有最佳答案,请猛点这里访问。
你好,我正在尝试从一本书《C++》杰西自由和Jim Keogh的编程入门中学习C++。我正在做第12章多重继承的问题。Q4要求我从以汽车为ADT的车辆中导出汽车和公共汽车,然后从
undefined reference to 'vtable for Coupe' on the constructor and destructor for
Coupe
请任何人告诉我我做错了什么,这样我可以了解更多关于如何使用vtables正确处理虚拟函数定义的信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #include <iostream> using namespace std; class Vehicle { public: Vehicle(){}; virtual ~Vehicle(){}; virtual int GetItsSpeed() = 0; int GetItsTyreSize(); virtual int GetItsRegistration() { return ItsRegistration; } protected: int ItsSpeed; int ItsRegistration; }; class Car : public Vehicle { public: Car(){}; virtual ~Car(){}; virtual int GetItsSpeed() = 0; int GetItsBootSize() { return ItsBootSize; } int GetItsRegistration() { return ItsRegistration; } virtual int GetItsRadioVolume() = 0; int GetItsTyreSize() { return ItsTyreSize; } protected: int ItsBootSize; int ItsRadioVolume; int ItsTyreSize; }; class Bus : public Vehicle { Bus(){}; ~Bus(){}; public: int GetItsSpeed() { return ItsSpeed; } int GetItsPassengerSize() { return ItsPassengerSize; } private: int ItsPassengerSize; }; class SportsCar : public Car { public: SportsCar(){}; ~SportsCar(){}; int GetItsSpeed() { return ItsSpeed; } int GetItsPowerSteeringAccuracy() { return ItsPowerSteeringAccuracy; } private: int ItsPowerSteeringAccuracy; }; class Wagon : public Car { public: Wagon(){}; ~Wagon(){}; int GetItsSpeed() { return ItsSpeed; } int GetItsTrailerSize() { return ItsTrailerSize; } private: int ItsTrailerSize; }; class Coupe : public Car { public: Coupe(){ ItsTyreSize = 10; } ~Coupe(){}; int GetItsSpeed(); int GetItsInteriorStyle() { return ItsInteriorStyle; } int GetItsRadioVolume() { return ItsRadioVolume; } private: int ItsInteriorStyle; }; void startof() { Coupe MariesCoupe; cout <<"Maries Coupe has a tyre size of" << MariesCoupe.GetItsTyreSize() <<" . "; } int main() { startof(); return 0; } |
您应该在
1 | int Coupe::GetItsSpeed() {return 0;}; |