1

Possible Duplicate:
Undefined reference to vtable

Running the following code is causing a linker error

'undefined reference to vtable for ManagedObjects'

#include <cstdlib>
#include <iostream>
#include <list>
#include <string>
using namespace std;

class ManagedObjects
{
      public:
      virtual string get() const;
      virtual ~ManagedObjects(){};
};

class CallbackOwner1 : public ManagedObjects
{
public:
       string get() const {return "CallbackOwner1";} 
};

class CallbackFunctor
{
public:
    CallbackFunctor(const ManagedObjects* b):m_cbr(b)
    {}
    string operator() ()
    {
        return m_cbr->get();
    }

   const ManagedObjects* m_cbr;
};

int main(int argc, char *argv[])
{
    ManagedObjects* cb1 = new CallbackOwner1();
    CallbackFunctor f(cb1);
    cout << f() << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}
Community
  • 1
  • 1
Kam
  • 5,878
  • 10
  • 53
  • 97

1 Answers1

2

You do not define the virtual string ManagedObjects::get() const method anywhere. Either define it as a pure virtual function virtual string get() const = 0; or provide a declaration for it.

  • Ahh Thank you! I don't know how I missed that – Kam Dec 16 '12 at 17:13
  • Read here: http://gcc.gnu.org/faq.html#vtables - the vtable is generated wherever the first virtual non-inline method is declared. In case that you had declared both get() and methodX() (in that order), with definitions elsewhere, had a definition for get() but no definition for methodX, the error message would have been much saner. – Antti Haapala -- Слава Україні Dec 16 '12 at 17:20