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();
};
4
  • 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). Commented Mar 30, 2013 at 15:05
  • possible duplicate of Undefined reference to vtable in Qt Commented Mar 30, 2013 at 15:06
  • Also, C++ Undefined Reference to vtable and inheritance Commented Mar 30, 2013 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. Commented Mar 30, 2013 at 15:10

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

8 Comments

Your example will not give any error unless you actually call the print method.
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.
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.
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 ....
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.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 until method invocation.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.