简单继承获取Vtable错误

时间:2011-10-18 18:26:17

标签: c++ eclipse eclipse-cdt

所以我正在尝试编译这个简单的代码:

// In Test.h
#include <iostream>
using namespace std;

class A{

public:
    virtual string f (string&);
};

class B : public A{

public:
    B (string);
    string f (string&);
};

// In Test.cpp
#include <iostream>
#include "Test.h"
using namespace std;


B :: B (string row){
    cout << "HERE";
}

string B :: f (string& x){
    cout << "Test";
}

看起来很简单,但我一直遇到Undefined reference to 'vtable for A'错误(编译器是Eclipse IDE的MINGW)。当我取出B的构造函数实现时,代码编译得很好。我缺少什么或这是链接器错误?

1 个答案:

答案 0 :(得分:1)

我认为您必须通过在声明末尾写A::f(string&)来提取= 0摘要,或实际提供A::f的实现

由于当前虚函数A::f不存在,编译器无法为其创建vtable(虚函数表)。