1

可能重复:
对 vtable 的未定义引用

运行以下代码会导致链接器错误

'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;
}
4

1 回答 1

2

您没有在virtual string ManagedObjects::get() const任何地方定义方法。要么将其定义为纯虚函数virtual string get() const = 0;,要么为其提供声明。

于 2012-12-16T17:11:37.650 回答