Consider following code:
myclass.h :
template <class T> struct S { void f(); }; struct MyClass : public S<int> { void g(); }; myclass.cpp :
#include "myclass.h" #include <iostream> template <class T> void S<T>::f() { std::cout << "f()\n"; /* some code here that could not be in header */ } void MyClass::g() { std::cout << "g()\n"; } main.cpp :
#include "myclass.h" int main() { MyClass m; m.g(); m.f(); // problem here } I've got linker error:
undefined reference to `S::f()'
Can I solve this issue without transferring implementation of S::f() to header file? Why S::f() not instantiated when I declare a MyClass derived from full specialized template base class?