【发布时间】:2016-09-26 16:24:44
【问题描述】:
您好,我正在尝试从 Jesse Liberty 和 Jim Keogh 的“C++ 编程入门”一书中学习 C++,我正在做第 12 章多重继承的问题。 Q4 要求我从以 car 作为 adt 的车辆派生 car 和 bus,然后从 car 派生 sportscar、wagon 和 coupe,并在 car 中从 vehicle 实现非纯虚函数。它正在codelite上编译,但在构建时它给出了错误
Coupe的构造函数和析构函数中未定义对“vtable for Coupe”的引用
请谁能告诉我我做错了什么,这样我就可以了解更多关于如何使用 vtables 正确处理虚函数定义的信息。
#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() << " .\n\n";
}
int main()
{
startof();
return 0;
}
【问题讨论】:
-
这里看起来不错:ideone.com/NTxKcK
标签: c++