c++ - Template class with virtual member: linker error -
this question has answer here:
consider following code. abstract, generic class; b both implements , specializes it. code seems trivially correct me, reason end strange linker errors.
template<typename t> class { 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 output:
/tmp/ccxg2z8a.o:(.rodata._ztv1aiie[_ztv1aiie]+0x10): undefined reference `a<int>::foo()' collect2: error: ld returned 1 exit status
clang output:
/tmp/l2-2a09ab.o: in function `main': l2.cpp:(.text+0x35): undefined reference `operator new(unsigned long)' /tmp/l2-2a09ab.o:(.rodata._zti1aiie[_zti1aiie]+0x0): undefined reference `vtable __cxxabiv1::__class_type_info' /tmp/l2-2a09ab.o:(.rodata._zti1b[_zti1b]+0x0): undefined reference `vtable __cxxabiv1::__si_class_type_info' /tmp/l2-2a09ab.o:(.rodata._ztv1aiie[_ztv1aiie]+0x10): undefined reference `a<int>::foo()' clang: error: linker command failed exit code 1 (use -v see invocation)
from gcc output, i'll assume function called foo
instead of f
.
the problem class a
not abstract, because haven't declared method such. in way:
virtual void foo() = 0;
but forgot = 0
, linker doesn't know method abstract, , therefore looking function body not there.
Comments
Post a Comment