简单继承获得虚值表错误

Simple Inheritance Gets Vtable Error

本文关键字:错误 继承 简单      更新时间:2023-10-16

所以我试着编译这个简单的代码:

// 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'错误(编译器是MINGW与Eclipse IDE)。当我取出B的构造函数实现时,代码编译得很好。我错过了什么,还是这是一个链接错误?

我认为您要么必须通过在声明末尾编写= 0来使A::f(string&)抽象,要么实际上提供A::f的实现

由于当前虚函数A::f不存在,编译器不能为它创建虚函数表