title: undefined reference to vtable for
body: <p>Basically, I'd like to create an interface (<code>C::doTask(double&amp;)</code>) using a virtual base class (<code>A</code>) and let the user to create a concrete class(<code>B</code>). Inside the interface, I use std::bind to pass a virtual struct member function. When compiling the code, I got the error of <code>undefined reference to vtable for XXX</code>. </p>
  
  <p>Any suggestions are appreciated. Thanks!</p>
  
  <p>Here is the header file <code>test.h</code></p>
  
  <pre><code>#include&lt;functional&gt;
  #include&lt;iostream&gt;
  
  
  typedef std::function&lt;double(double const &amp;)&gt; MyFun;
  
  // base class
  struct A
  {
      double x;
      // other parameters
      A(){ x = 0.;};
      virtual ~A();
      virtual double fun(double const &amp;x);
  };
  
  struct C
  {
      MyFun myfun;
      void init(A &amp;a)
      {
          using std::placeholders::_1;
          myfun = std::bind( &amp;A::fun, a, _1 );
      }
      double doTask(double x)
      {
          // do something
          // call myfun 
          return 0.;
      }
  };
  </code></pre>
  
  <p>Here is the <code>main</code> file <code>test.cpp</code></p>
  
  <pre><code>#include&lt;iostream&gt;
  #include "test.h"
  
  // concrete class
  struct B:A
  {
      // define some other parameters as members
      double a,b;
      B()
      {
          a=1.;
          b=2.;
      }
      double fun(double const &amp;x) override
      {
          return a+b+x;
      }
  };
  
  
  int main()
  {
      B b;
      C c;
      c.init(b);
  
      c.doTask(0.5);
      return 0;
  }
  </code></pre>
  
  <p>Compile <code>g++ --std=c++11 test.cpp</code> and I got:</p>
  
  <pre><code>/tmp/ccTmrVPi.o: In function `A::A()':
  test.cpp:(.text._ZN1AC2Ev[_ZN1AC5Ev]+0x9): undefined reference to `vtable for A'
  /tmp/ccTmrVPi.o: In function `std::_Head_base&lt;0ul, A, false&gt;::~_Head_base()':
  test.cpp:(.text._ZNSt10_Head_baseILm0E1ALb0EED2Ev[_ZNSt10_Head_baseILm0E1ALb0EED5Ev]+0x14): undefined reference to `A::~A()'
  /tmp/ccTmrVPi.o: In function `A::A(A const&amp;)':
  test.cpp:(.text._ZN1AC2ERKS_[_ZN1AC5ERKS_]+0xd): undefined reference to `vtable for A'
  /tmp/ccTmrVPi.o: In function `B::~B()':
  test.cpp:(.text._ZN1BD2Ev[_ZN1BD5Ev]+0x20): undefined reference to `A::~A()'
  /tmp/ccTmrVPi.o:(.rodata._ZTI1B[_ZTI1B]+0x10): undefined reference to `typeinfo for A'
  collect2: error: ld returned 1 exit status
  </code></pre>