我在使用纯虚方法和正向类声明时遇到问题。 以下程序可以编译,但不会链接。
class SubClass;
class SuperClass {
public:
virtual SubClass& GetSomething() = 0;
};
class SubClass: public SuperClass {
public:
SubClass& GetSomething();
};
SubClass& SubClass::GetSomething()
{
return *this;
}
int main()
{
SuperClass *obj = new SubClass();
SubClass result = obj->GetSomething();
return 0;
}
我遇到以下错误:
Undefined symbols for architecture x86_64:
"vtable for __cxxabiv1::__class_type_info", referenced from:
typeinfo for SuperClass in classtest-56e822.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for __cxxabiv1::__si_class_type_info", referenced from:
typeinfo for SubClass in classtest-56e822.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"operator new(unsigned long)", referenced from:
_main in classtest-56e822.o
"___cxa_pure_virtual", referenced from:
vtable for SuperClass in classtest-56e822.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我在做什么错了?