实现虚拟函数时对 vtable 的未定义引用
Undefined reference to vtable while implementing virtual functions
我尝试实现Jesse Liberty和Tim Keogh的"C++编程入门"的例子,但是实现纯虚函数是编译而不是构建。它给出了错误:未定义对"vtable for circle"的引用
我尝试将变量itsRadius
替换为虚拟函数GetItsRadius
以查看它是否有效,但它开始在switch语句中给我相同的错误,但对于Rectangle
和Square
,对于circle
也出现了与以前相同的错误。
这是代码:
#include <iostream>
using namespace std;
enum BOOL { FALSE, TRUE };
class Shape
{
public:
Shape(){}
~Shape(){}
virtual long GetArea() = 0; // error
virtual long GetPerim()= 0;
virtual void Draw() = 0;
private:
};
void Shape::Draw()
{
cout << "Abstract drawing mechanism!n";
}
class Circle : public Shape
{
public:
Circle(int radius):itsRadius(radius){}
~Circle(){}
long GetArea() { return 3 * itsRadius * itsRadius; }
long GetPerim() { return 9 * itsRadius; }
void Draw();
private:
int itsRadius;
int itsCircumference;
};
class Rectangle : public Shape
{
public:
Rectangle(int len, int width):
itsLength(len), itsWidth(width){}
~Rectangle(){}
long GetArea() { return itsLength * itsWidth; }
long GetPerim() { return 2*itsLength + 2*itsWidth; }
virtual int GetLength() { itsLength; }
virtual int GetWidth() { itsWidth; }
void Draw();
private:
int itsWidth;
int itsLength;
};
void Rectangle::Draw()
{
for (int i = 0; i<itsLength; i++)
{
for (int j = 0; j<itsWidth; j++)
cout << "x ";
cout << "n";
}
Shape::Draw();
}
class Square : public Rectangle
{
public:
Square(int len);
Square(int len, int width);
~Square(){}
long GetPerim() {return 4 * GetLength();}
};
Square::Square(int len):
Rectangle(len,len)
{}
Square::Square(int len, int width):
Rectangle(len,width)
{
if (GetLength() != GetWidth())
cout << "Error, not a square... a Rectangle??n";
}
void startof()
{
int choice;
BOOL fQuit = FALSE;
Shape * sp;
while (1)
{
cout << "(1)Circle (2)Rectangle (3)Square (0)Quit: n";
cin >> choice;
switch (choice)
{
case 1: sp = new Circle(5);
break;
case 2: sp = new Rectangle(4,6);
break;
case 3: sp = new Square (5);
break;
default: fQuit = TRUE;
break;
}
if (fQuit)
break;
sp->Draw();
cout << "n";
}
}
int main()
{
startof();
}
这里的问题是你还没有定义函数Circle::Draw()
。如果定义它,代码将编译。
顺便说一句,您应该使Shape
析构函数成为虚拟的(否则在某些情况下可能无法正确销毁派生类的实例)。这里的规则是,如果一个类有任何虚拟成员,析构函数也应该是虚拟的。
我知道你看到的编译消息非常神秘。我见过这种"未定义的向量表"编译器错误是其他一些问题的副作用的情况,就像在这种情况下一样。只是为了让您知道,消除此错误的一个好方法是将受影响类的虚函数定义之一移出行(我使用上面的 Circle 析构函数执行此操作,首先使 Shape 析构函数成为虚拟)。这揭示了更好的编译器错误的真正问题。
相关文章:
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用