Source:
test.hpp
template<class R>
class Base
{
public:
virtual R copy() = 0;
virtual ~Base() = default;
R test();
R testCallback();
};
class Derived: public Base<Derived>
{
public:
Derived()
{
}
Derived copy();
};
test.cpp
#include "test.hpp"
template<class R>
R Base<R>::testCallback()
{
return R();
}
template<class R>
R Base<R>::test()
{
return testCallback();
}
Derived Derived::copy()
{
return Derived();
}
app.cpp
#include "test.hpp"
int main(int argc, char const *argv[])
{
Derived a = Derived();
Derived b = a.test();
return 0;
}
For app compiling I use:
g++ source/test.cpp source/app.cpp -o test
And I get the error afterward:
Undefined reference to «Base::test()»
It works if I cut all code from test.cpp and put it to app.cpp. It also works if I mark R test() as a virtual function in the header file.
Why the linker doesn't see the test() implementation function? What have I done wrong?