Undefined reference to 'vtable for

4

我认为需要注意的是,这段代码在Visual Studio 2012上可以编译通过,但在g++上无法通过。在实现RSA.cpp之前,它甚至能够完美编译通过。自那以后我没有修改头文件。错误信息如下:

cipher.o: In function `RSA_433::RSA_433()':
/home/andrew/Desktop/ciphers/RSA.h:18 undefined reference to `vtable for RSA_433'
collect2: error: ld returned 1 exit status

以下是RSA.h文件:

class RSA_433: public CipherInterface
{

public:


    RSA_433(){}
    virtual bool setKey(const unsigned char* key);

    virtual unsigned char* encrypt(const unsigned char* plaintext);

    virtual unsigned char* decrypt(const unsigned char* ciphertext);

protected:

    RSA* RSAkey;
    const char* keyFileName;
};

以及RSA.cpp:

//RSA_433::RSA_433(){}  removed

bool RSA_433::setKey(const unsigned char* key){//body}

unsigned char* RSA_433::encrypt(const unsigned char* plaintext){//body}

unsigned char* RSA_433::decrypt(const unsigned char* ciphertext){//body}

RSA.h还继承了以下内容:

class CipherInterface
{
public:

    CipherInterface(){}

    virtual bool setKey(const unsigned char* key){ return false;  }

    virtual unsigned char* encrypt(const unsigned char* plaintext){ return NULL; }

    virtual unsigned char* decrypt(const unsigned char* ciphertext) { return NULL; }


};

1
你确定这就是整个的 CipherInterface 吗?没有漏掉析构函数吧?(可能未定义,纯虚?) - Luchian Grigore
听起来你没有链接RSA.o文件。 - aschepler
@Luchian Grigore 是的,就是这样。之前它运行得很好。 - Andrew Tsay
@aschepler 我在我的 make 文件中链接了它。在我实现 rsa.cpp 之前,它已经编译过了。 - Andrew Tsay
1
不确定你的g++安装出了什么问题,但在我看来代码是没问题的,而且在cygwin/g++环境下也能正常编译和链接。 - R Sahu
显示剩余2条评论
2个回答

3
您似乎有两个关于 RSA_433::RSA_433() 的定义,一个在 .h 文件中,另一个在 .cpp 文件中。
此外,CipherInterface 类中定义的所有方法都是内联的。根据 GCC 标准

ISO C++ 标准规定:类的所有非纯虚拟方法必须被定义,但不要求对违反此规则的情况进行任何诊断 [class.virtual]/8。基于这个假设,GCC 只会在定义了其首个非内联的虚拟成员函数的翻译单元中发出隐式定义的构造函数、赋值运算符、析构函数和虚表。

这意味着 GCC 仅会在第一个非纯虚拟且非内联虚成员函数的定义所在的翻译单元中包含虚函数表。如果没有这样的定义,则不会为该类包含虚函数表。

1
抱歉,那是我添加的尝试修复它的。忘记删除它了。仍然不起作用 :/ - Andrew Tsay

0
答案很简单:您应该在 CipherInterface 中添加和实现虚析构函数,问题就会消失。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接