c++ - 实现虚函数时未定义对 vtable 的引用

标签 c++

<分区>

我尝试实现 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 析构函数成为虚拟的(否则派生类的实例在某些情况下可能无法正确销毁)。这里的规则是,如果一个类有 任何 个虚拟成员,那么析构函数也应该是虚拟的。

我知道您看到的编译消息非常隐秘。我已经看到这种“未定义的 v 表”编译器错误是其他一些问题的副作用的情况,就像在这种情况下一样。只是让您知道,消除此错误的一个好方法是将受影响类的虚函数定义之一移出行外(我用上面的 Circle 析构函数执行此操作,首先将 Shape 析构函数设为 virtual)。这揭示了更好的编译器错误的真正问题。

关于c++ - 实现虚函数时未定义对 vtable 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37459746/

相关文章:

c++ - boost read_xml 问题解析字符串?

c++ - 存储类初始化的模板

c++ - 程序收到信号 SIGSEGV,Segmentation fault

c++ - 这个极其简单的线程监视器实现有什么不安全的地方?

c++ - 从接口(interface)序列化派生类

c++ - MessageBox 超时或从另一个线程关闭 MessageBox

C++ - 内存泄漏 - 指向结构的指针

c++ - 指向同一个位置的两个指针?

c++ - 将 C++ builder 6 项目转换为 C++ builder 2010

c++ - volatile 变量存储在哪里?