1

I want to inherit QObject and another class and got an error: undefined reference for `vtable for EduGraph' I've read some threads about it and have fixed the sequence of the inherited classes in the class definition, but it haven't solved the problem.

class EduGraph : public QObject, public Graph<Vertex<ENode, EEdge>*> {
private:
    std::list<Vertex<ENode, EEdge>*>::iterator firstSel;
    std::list<Vertex<ENode, EEdge>*>::iterator secSel;
public:

Q_OBJECT

    EduGraph() : firstSel(0), secSel(0) {}
    ~EduGraph();

    void NewNode(const QPoint& p);
    void RemoveNode();
    void Associate();
    void Dissociate();

signals:
    void VertexSelected();
    void VertexDeSelected();
};
Müller András
  • 191
  • 3
  • 12
  • Are you sure you need multiple inheritance? From the above code, using composition looks like it would work (i.e. have a `Graph<...>` member in your class rather than inheriting from it). – Mat Mar 30 '13 at 15:05
  • possible duplicate of [Undefined reference to vtable in Qt](http://stackoverflow.com/questions/15656902/undefined-reference-to-vtable-in-qt) – Alok Save Mar 30 '13 at 15:06
  • Also, [C++ Undefined Reference to vtable and inheritance](http://stackoverflow.com/questions/9406580/c-undefined-reference-to-vtable-and-inheritance) – Alok Save Mar 30 '13 at 15:07
  • 1
    Does rerunning qmake help? Also, the Q_OBJECT macro is technically supposed to be in the private section of your class definition. Customarily, it's the first thing after the opening curly brace. – Chris Mar 30 '13 at 15:10

1 Answers1

2
`Undefined reference to `vtable for...'` 

is usually a sign of unimplemented virtual function. Make sure you have implemented (defined) the corresponding virtual functions you inherited from the base classes.

For example this will give you the same error because the print method in B is not implemented.

class A {
public:
    virtual void print() = 0;
};

class B : public A{
public:
    void print();
};

int main()
{
    B b;
}
  • Your example will [not give any error](http://ideone.com/N8acL1) unless you actually call the `print` method. – Alok Save Mar 30 '13 at 15:19
  • It does give an error message on gcc 4.4.0 at least. **You don't get an error message if you don't declare an instance of B**. Hence why i have put a declarion in main. –  Mar 30 '13 at 15:22
  • First of all that is (gcc-4.7.2). Plus even if that was the same version, what you are trying to do is tell me that what I am looking at right here on my terminal is a result of my imagination. Maybe it is but I doubt it. –  Mar 30 '13 at 15:41
  • Instead of just saying it doesn't you can atleast try to explain why it shouldn't or if it is caused by some configuration .... –  Mar 30 '13 at 15:42
  • The rule says *cannot create any objects of an Abstract class*.An abstract class has atleast one pure virtual function.Without the overidding method in `B`,Your class autmotically becomes a Abstract class and compiler will complain if you [just create an instance](http://ideone.com/rtjLXQ).Once you override the method `print()`. You instruct the compiler,My class is not Abstract & promise the compiler to provide its definition whilst the linking.This is similar to declaring a method and not defining it, [no error](http://ideone.com/RwLUik) until [method invocation](http://ideone.com/uWUsp6). – Alok Save Mar 30 '13 at 15:56
  • You can make the base class non abstract. `virtual void print() {}`. I still get the same error. I can even add other virtual methods which I inherit and define properly. However as long as `print` is inherited but not defined the error stays. –  Mar 30 '13 at 16:07
  • And `undefined reference to A::print()'` and `undefined reference to vtable for B'` are not the same errors. –  Mar 30 '13 at 16:08
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27239/discussion-between-ras-and-alok-save) –  Mar 30 '13 at 16:10