Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ virtual function undefined at link time - why?
    text
    copied!<p>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.</p> <p>I have the following class:</p> <pre><code>template&lt;class BufferType, class ConnectionType, class HandlerType&gt; class UdpConnection { public: UdpConnection(size_t dispatchCount) : service(), listener(service), pool(dispatchCount), sysMsgHandlers(), bufferPool(), buffers() { assert(dispatchCount &gt; 0); initBuffers(dispatchCount); initSysHandlers(); } protected: virtual void initSysHandlers() = 0; } </code></pre> <p>In my subclass:</p> <pre><code>class UdpClient : public UdpConnection&lt;SyncBufferHandler, UdpClient, ClientNetworkHandler&gt; { protected: void initSysHandlers(); } </code></pre> <p>And the subclass source file:</p> <pre><code>void UdpClient::initSysHandlers() { } </code></pre> <p>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. </p> <pre><code>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&lt;voip::network::client::SyncBufferHandler, voip::network::client::UdpClient, voip::network::client::ClientNetworkHandler&gt;::UdpConnection(unsigned long)': udpclient.cpp:(.text._ZN4voip7network13UdpConnectionINS0_6client17SyncBufferHandlerENS2_9UdpClientENS2_20ClientNetworkHandlerEEC2Em[voip::network::UdpConnection&lt;voip::network::client::SyncBufferHandler, voip::network::client::UdpClient, voip::network::client::ClientNetworkHandler&gt;::UdpConnection(unsigned long)]+0x10d): undefined reference to `voip::network::UdpConnection&lt;voip::network::client::SyncBufferHandler, voip::network::client::UdpClient, voip::network::client::ClientNetworkHandler&gt;::initSysHandlers()' collect2: ld returned 1 exit status </code></pre> <p>What am I doing wrong here? Please ask if you need more information, wanted to keep it as short as possible!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload