0
#include <QGraphicsScene>

using namespace std;

class Object
{
    public:
    Object(){};
    virtual ~Object(){};
    virtual void draw(QGraphicsScene * s, int x, int y){};
    virtual string get();
};

I get an error saying "undefined reference to vtable for Object". The error happens on both the constructor and the destructor. The error goes away when I delete the "using namespace std;" line. How can I fix this error without deleting that line? Or providing another method of using a string variable type?

  • Did you include or use a STL library? – Joel Nov 19 '15 at 00:42
  • The code is ill-formed in either case so you should fix the problem anyway (by providing bodies for your virtual functions). – M.M Nov 19 '15 at 00:53
  • It won't solve your problem, but you're probably better off deleting `using namespace std;` It can cause all sorts of weird errors. Read more here: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – user4581301 Nov 19 '15 at 00:54

1 Answers1

1

The error occurs because a virtual method is declared but not defines, in your situation that's

virtual string get();

And somewhere in your code you are telling to the compiler to emit the vtable for Object by instantiating it, eg

Object* o = new Object();

You must define it, or if you want to let subclasses implement it, explicitly mark it as pure:

virtual string get() = 0;

In both situations (letting it unimplemented or mark it as pure) you won't be able to directly instantiate an Object instance, because the object has an incomplete implementation.

Jack
  • 131,802
  • 30
  • 241
  • 343