【问题标题】:Undefined reference to vtable in final class constructor and destructor [duplicate]最终类构造函数和析构函数中对 vtable 的未定义引用 [重复]
【发布时间】:2016-09-26 16:24:44
【问题描述】:

您好,我正在尝试从 Jesse Liberty 和 Jim Keogh 的“C++ 编程入门”一书中学习 C++,我正在做第 12 章多重继承的问题。 Q4 要求我从以 car 作为 adt 的车辆派生 car 和 bus,然后从 car 派生 sportscarwagoncoupe,并在 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;
}

【问题讨论】:

标签: c++


【解决方案1】:

您应该在Coupe 中实现GetItsSpeed。 例如,

int Coupe::GetItsSpeed() {return 0;};

【讨论】:

  • 非常感谢,非常有帮助。
猜你喜欢
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
相关资源
最近更新 更多