S
S
sig2011-10-06 11:02:37
Qt
sig, 2011-10-06 11:02:37

Framework Qt 4.7. Problem when generating virtual table pointers?

Let's start in order. There is a description of a certain class responsible for the visual design of the program.

#include <QtGui>
class Windows : public QObject
{
protected:
   friend class MyClass;

   QPushButton *but1;
   QPushButton *but2; 
public:
   Windows(QApplication *App);
   QPushButton *RUN;
}

In the constructor of this class, all fields are initialized. QApplication*App is needed in order to close the application via the “Close” button click signal.
Next comes the description of the friend class, in which the aggregation of the previously described class by value (composition) will be observed.
#include <windows.h>
#include <QFile>
#include <cstdlib>
class MyClass : public QObject
{
    Q_OBJECT
private:
    int number_of_lines;
public:
    MyClass (QApplication*App);
    Windows *win; // Объект, отвечающий за визуализацию
public slots:
    void myFunc(); 
}

The definition of this class looks like this:
MyClass:: MyClass (QApplication *App) // Конструктор
{
    win = new Windows (App);
}
void MyClass::myFunc()
{  }

The body of the main program main.cpp contains the following:
#include "myclass.h"
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MyClass *c1 = new MyClass (&app);

    QObject::connect(c1->win->RUN, SIGNAL(clicked()),
                     c1, SLOT(myFunc()));
    return app.exec();
}

That is, when you press the RUN button in the program window, the myFunc () function from the c1 object should (in theory) be launched.
When I run compilation in the editor, I get the following error:
In function "MyClass":
undefined reference to "vtable for Compiler"
(here is a reference to the constructor of the MyClass class)
undefined reference to "vtable for Compiler"
(duplicate)
collect2: returned 1 exit status

I ask for help in solving this problem. I already tried to solve it in a similar way a month ago, but having met such an error, I abandoned this idea. He continued to work according to the principle: he did not put any links in main.cpp, but directly called the function (not the slot) from the object:
#include "myclass.h"
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MyClass *c1 = new MyClass (&app);
    c1->myFunc();
}

That is, the program started working with the function before starting, and when the RUN button was pressed, it simply showed a window with the contents of the result.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Sidorov, 2011-10-06
@sig

>MyClass:: MyClass(QApplication *App) // Constructor
Mother my woman! qapp is a singleton! Friend classes in this particular case are not needed at all and are rather harmful. And I also advise you to smoke on the topic of moc, in main.cpp if you really want to declare a class dependent on the QObject file, then you need to write #include "moc_main.cxx" at the end or same #include "main.moc"
And in general, all the code is not written in the spirit of Qt, but in some strange way. It's better to read some cool examples for starters and in the coding conventions docs!

P
Paul, 2011-10-06
@Paul

Did you forget to wet the MyClass.h header?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question