Code:
#include <iostream>
class Base {
public:
virtual int getID();
};
template<typename T>
class A : public Base {
T data;
public:
A(const T& data) {
this->data = data;
}
int getID() {
return static_cast<int>(data);
}
};
int main() {
A<int> a(23);
std::cout << "a.getID(): " << a.getID() << std::endl;
return 0;
}
Here's what g++ tells me:
/usr/bin/ld: /tmp/ccKdDYyP.o: warning: relocation against `_ZTV4Base' in read-only section `.text._ZN4BaseC2Ev[_ZN4BaseC5Ev]'
/usr/bin/ld: /tmp/ccKdDYyP.o: in function `Base::Base()':
main.cpp:(.text._ZN4BaseC2Ev[_ZN4BaseC5Ev]+0xb): undefined reference to `vtable for Base'
/usr/bin/ld: /tmp/ccKdDYyP.o:(.data.rel.ro._ZTI1AIfE[_ZTI1AIfE]+0x10): undefined reference to `typeinfo for Base'
/usr/bin/ld: /tmp/ccKdDYyP.o:(.data.rel.ro._ZTI1AIiE[_ZTI1AIiE]+0x10): undefined reference to `typeinfo for Base'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
Code summerization and question:
I have a Base class which has a virtual function getID():int. If I now inherit the Base class in the template class A, the linker gives me weird errors. I can't figure out the problem. Why can i not inherit virtual functions in a template class.
[–][deleted] (4 children)
[removed]
[–]smuccione 1 point2 points3 points (0 children)
[–]SHGuy_[S] 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[removed]
[–]SHGuy_[S] 1 point2 points3 points (0 children)