I’ve defined a public template function in a class called EventDispatcher:
public:
template <class className> void AddEventListener ( className* );
The function implements like this:
template <class className>
void EventDispatcher::AddEventListener ( className* ptr )
{
};
Then in some class I create an insatnce of Timer which is a child EventDispatcher and call AddEventListener:
timer->AddEventListener ( this );
After what I’m getting an error
/tmp/ccjRF7sK.o: In function `AirHockeyServer::AirHockeyServer()':
AirHockeyServer.cpp:(.text+0xa5): undefined reference to `void EventDispatcher::AddEventListener<AirHockeyServer>(AirHockeyServer*)'
/tmp/ccjRF7sK.o: In function `AirHockeyServer::AirHockeyServer()':
AirHockeyServer.cpp:(.text+0x187): undefined reference to `void EventDispatcher::AddEventListener<AirHockeyServer>(AirHockeyServer*)'
collect2: ld returned 1 exit status
This is a linker error that tells you that the linker does not see the implementation of the
AddEventListenermember function. With templates it is common to put the implementation of functions in the header file, so they can be instantiated for any template parameter argument.