调用基类的构造函数时对 vtable 的未定义引用

Joh*_*y-T 0 c++ methods inheritance constructor vtable

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

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;
};
Run Code Online (Sandbox Code Playgroud)

这是从上面继承的类:

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);

};
Run Code Online (Sandbox Code Playgroud)

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

CProcManager init_ccpu((uint8_t*)mem, 32/*just a test number*/);
Run Code Online (Sandbox Code Playgroud)

我得到的错误信息:

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
Run Code Online (Sandbox Code Playgroud)

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

Mik*_*our 5

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

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