实现虚拟函数时对 vtable 的未定义引用

Undefined reference to vtable while implementing virtual functions

本文关键字:未定义 引用 vtable 虚拟 函数 实现      更新时间:2023-10-16

我尝试实现Jesse Liberty和Tim Keogh的"C++编程入门"的例子,但是实现纯虚函数是编译而不是构建。它给出了错误:未定义对"vtable for circle"的引用

我尝试将变量itsRadius替换为虚拟函数GetItsRadius以查看它是否有效,但它开始在switch语句中给我相同的错误,但对于RectangleSquare,对于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 析构函数成为虚拟)。这揭示了更好的编译器错误的真正问题。