【问题标题】:undefined reference to vtable when calling constructor of base class调用基类的构造函数时对 vtable 的未定义引用
【发布时间】:2014-06-05 10:22:33
【问题描述】:

我正在为学校做一个项目,但我遇到了一个不知道如何解决的问题。以下是部分代码(不是整个类)和错误消息:

class CCPU
{
public:

                         CCPU                          ( uint8_t         * memStart,
                                                         uint32_t          pageTableRoot );
virtual                 ~CCPU                          ( void ) { }
virtual uint32_t         GetMemLimit                   ( void ) const = 0;
virtual bool             SetMemLimit                   ( uint32_t          pages ) = 0;
virtual bool             NewProcess                    ( void            * processArg,
                                                         void           (* entryPoint) ( CCPU *, void * ),
                                                         bool              copyMem ) = 0;

bool                     ReadInt                       ( uint32_t          address,
                                                         uint32_t        & value );
bool                     WriteInt                      ( uint32_t          address,
          m_PageTableRoot;
};

这是从上面继承的类:

class CProcManager : public CCPU
{
public:

CProcManager( uint8_t* memStart,uint32_t pageTableRoot ) : CCPU(memStart, pageTableRoot) {}
virtual uint32_t         GetMemLimit                   ( void ) const;
virtual bool             SetMemLimit                   ( uint32_t          pages );
virtual bool             NewProcess                    ( void            * processArg,
                                                         void           (* entryPoint) ( CCPU *, void * ),
                                                         bool              copyMem );
static void              InitInfoPages                 (uint8_t * pages_mem);

};

这将是调用构造函数的代码:

CProcManager init_ccpu((uint8_t*)mem, 32/*just a test number*/);

我得到的错误信息:

solution.o: In function `CProcManager::CProcManager(unsigned char*, unsigned int)':
/home/Jan/OSY/OSY-2/solution.cpp:19: undefined reference to `vtable for CProcManager'
solution.o: In function `CProcManager::~CProcManager()':
/home/Jan/OSY/OSY-2/solution.cpp:15: undefined reference to `vtable for CProcManager'
collect2: error: ld returned 1 exit status
make: *** [test1] Error 1

CCPU 和 CProcManager 类的所有方法都已定义,我不应该更改 CCPU 类(该类在项目分配中提供)。 有人可以向我解释问题出在哪里(我猜它有定义)?

【问题讨论】:

    标签: c++ inheritance methods constructor vtable


    【解决方案1】:

    这很可能意味着您忘记实现CProcManager 中声明的虚函数之一——可能是GetMemLimit。或者您忘记与包含该实现的翻译单元链接。

    解释:看起来您正在使用 GCC 进行编译。该编译器在与类中声明的第一个非内联、非纯虚函数相同的翻译单元中生成 vtable。如果您不实现该功能,则 vtable 将丢失,并给出该错误。

    【讨论】:

    • 感谢您的回答,问题是 CProcManager 中的虚拟方法没有定义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 2020-07-07
    • 2016-09-26
    • 2015-05-25
    • 1970-01-01
    相关资源
    最近更新 更多