#include <stdio.h>
class A {
public:
virtual void print();
A();
};
class B :public A {
public:
void print();
B();
};
class C :public B {
public:
void print();
C();
};
A::A(){
}
B::B(){
}
C::C(){
}
void B::print() {
printf("From B\n");
}
void C::print() {
printf("From C\n");
}
int main() {
B* object = new C;
object->print();
return 0;
}
當我嘗試編譯此C++文件時,出現以下錯誤。無法弄清原因。我嘗試閱讀SO上類似的未定義的vtable問題。未定義的vtable C++
/tmp/ccpOkVJb.o: In function `A::A()':
test1.cpp:(.text+0xf): undefined reference to `vtable for A'
/tmp/ccpOkVJb.o:(.rodata._ZTI1B[_ZTI1B]+0x10): undefined reference to `typeinfo for A'
collect2: error: ld returned 1 exit status
你應該更加小心 – Maxwe11