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?

3
  • Did you include or use a STL library? Commented Nov 19, 2015 at 0:42
  • The code is ill-formed in either case so you should fix the problem anyway (by providing bodies for your virtual functions). Commented Nov 19, 2015 at 0: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: stackoverflow.com/questions/1452721/… Commented Nov 19, 2015 at 0:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.