<分区>
考虑以下代码。 A 是一个抽象的泛型类; B既实现又专门化它。这段代码对我来说似乎完全正确,但出于某种原因,我最终遇到了奇怪的链接器错误。
template<typename T>
class A {
public:
virtual void f();
};
class B : public A<int> {
public:
void f() {};
};
int main(int argc, char** argv) {
auto b = new B();
return 0;
}
gcc 输出:
/tmp/ccXG2Z8A.o:(.rodata._ZTV1AIiE[_ZTV1AIiE]+0x10): undefined reference to `A<int>::foo()'
collect2: error: ld returned 1 exit status
clang 输出:
/tmp/l2-2a09ab.o: In function `main':
l2.cpp:(.text+0x35): undefined reference to `operator new(unsigned long)'
/tmp/l2-2a09ab.o:(.rodata._ZTI1AIiE[_ZTI1AIiE]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
/tmp/l2-2a09ab.o:(.rodata._ZTI1B[_ZTI1B]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/tmp/l2-2a09ab.o:(.rodata._ZTV1AIiE[_ZTV1AIiE]+0x10): undefined reference to `A<int>::foo()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)