I am trying to define the overloaded member functions of a templated derived class in a separate .cc file. The code structure is as follows:
//header.hh
class Base
{
public:
virtual void visit () = 0;
};
template <class T>
class Derived : public Base
{
public:
void visit () override;
};
//impl.cc
template <class T>
Derived<T>::visit ()
{
return;
}
I use an instance of the derived class as a member variable in other class
//usecaseh.hh
class Usecase
{
Derived <Usecase> d;
};
//usecase.cc
Usecase::Usecase ()
{
d = new Derived <Usecase> ();
}
However while compilation , I exit with the following error
undefined reference to non-virtual thunk Derived<Usecase>::visit()
I am not really sure why this is happening. The code snippet shared is just a high level abstraction of the design. In practice, the Base class is a graph traversal iterator, and I plug in my Usecase class to walk a graph and do some stuff on it via the visit method.
- Why is the compiler not able to reconcile the definition and declaration of visit method?
- How do I resolve this