简单继承出现 Vtable 错误
所以我试图编译这个简单的代码:
// 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 的构造函数实现时,代码可以正常编译。我错过了什么或者这是链接器错误?
So I am trying to compile this simple code:
// 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";
}
It seems simple enough but I keep getting a Undefined reference to 'vtable for A'
error (compiler is MINGW with Eclipse IDE). When I take out the constructor implementation for B, the code compiles fine. What am I missing or is this a linker error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你要么必须通过在声明末尾写入
= 0
来使A::f(string&)
抽象,或者实际上提供的实现A::f
由于当前虚拟函数
A::f
不存在,编译器无法为其创建vtable(虚函数表)。I think you either have to make
A::f(string&)
abstract by writing= 0
at the end of the declaration, or actually provide an implementation ofA::f
Since currently the virtual function
A::f
does not exist, the compiler cannot create a vtable for it (the virtual function table).