I'm having a bit of trouble using virtual functions in C++, and I might be misusing them in a constructor. The problem is that when linking a component lib (written by me) into my final executable, a virtual function is marked as undefined, even though I have written an implementation for it, and linked it.
I have the following class:
template<class BufferType, class ConnectionType, class HandlerType>
class UdpConnection
{
public:
UdpConnection(size_t dispatchCount) : service(),
listener(service),
pool(dispatchCount), sysMsgHandlers(),
bufferPool(), buffers()
{
assert(dispatchCount > 0);
initBuffers(dispatchCount);
initSysHandlers();
}
protected:
virtual void initSysHandlers() = 0;
}
In my subclass:
class UdpClient : public UdpConnection<SyncBufferHandler, UdpClient, ClientNetworkHandler>
{
protected:
void initSysHandlers();
}
And the subclass source file:
void UdpClient::initSysHandlers()
{
}
As you can see, I am calling a virtual function in my constructor. As far as I can tell, this should be fine, since I am aware that my subclass constructor won't have been called, so I can't use any instance variables, but I simply add a few sub-class specific items to a std::map.
Linking CXX static library libnetwork.a
[ 75%] Built target network
Scanning dependencies of target testclient
[ 87%] Building CXX object CMakeFiles/testclient.dir/src/test/testclient.cpp.o
Linking CXX executable testclient
src/network/libnetwork.a(udpclient.cpp.o): In function `voip::network::UdpConnection<voip::network::client::SyncBufferHandler, voip::network::client::UdpClient, voip::network::client::ClientNetworkHandler>::UdpConnection(unsigned long)':
udpclient.cpp:(.text._ZN4voip7network13UdpConnectionINS0_6client17SyncBufferHandlerENS2_9UdpClientENS2_20ClientNetworkHandlerEEC2Em[voip::network::UdpConnection<voip::network::client::SyncBufferHandler, voip::network::client::UdpClient, voip::network::client::ClientNetworkHandler>::UdpConnection(unsigned long)]+0x10d): undefined reference to `voip::network::UdpConnection<voip::network::client::SyncBufferHandler, voip::network::client::UdpClient, voip::network::client::ClientNetworkHandler>::initSysHandlers()'
collect2: ld returned 1 exit status
What am I doing wrong here? Please ask if you need more information, wanted to keep it as short as possible!