Undefited reference to 'vtable for Notepad'
-
I began to study QT programming and tries to create a simplest Notepad app.
So i write some code and when i try to launch it i see the error. It is seen in figure below.
How can i solve this problem?
List of code:
@
#include <QApplication>
#include <QTextEdit>
#include <QTGui>int main(int argv, char **args)
{
QApplication app(argv, args);QTextEdit *textEdit = new QTextEdit; QPushButton *quitButton = new QPushButton("Quit"); QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit())); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(textEdit); layout->addWidget(quitButton); QWidget window; window.setLayout(layout); window.show(); return app.exec();
}
class Notepad : public QMainWindow
{
Q_OBJECT
public:
Notepad();
private slots:
void open();
void save();
void quit();private: QTextEdit *textEdit; QAction *openAction; QAction *saveAction; QAction *exitAction; QMenu *fileMenu; };
Notepad::Notepad()
{}
@!http://s47.radikal.ru/i118/1203/ec/975990a071d2.png(Error)!
But when i delete constructor Notepad::Notepad(){...} i haven't errors...
-
[quote author="amleto" date="1330978130"]Not sure exactly what is wrong, but I'm pretty sure the problem goes away if you put class files/sources in files separate to main.
It might be related or not to your lack of implementation for those slots.[/quote]
You say that i must put my class in another file, not in main.cpp?
But how then i call this file in main function? If i understand wright... -
This is a linker message that is usually caused because of missing dependencies such as header files and/or macros definition or if you have not "moc":http://qt-project.org/doc/qt-4.8/moc.html your header files. I would also recommend you to split your source code into .h and .cpp files.
Btw a nice tutorial with an example of a simple notepad application is available "here":http://qt-project.org/doc/qt-4.8/gettingstartedqt.html.
-
[quote author="leon.anavi" date="1330980549"]This is a linker message that is usually caused because of missing dependencies such as header files and/or macros definition or if you have not "moc":http://qt-project.org/doc/qt-4.8/moc.html your header files. I would also recommend you to split your source code into .h and .cpp files.
Btw a nice tutorial with an example of a simple notepad application is available "here":http://qt-project.org/doc/qt-4.8/gettingstartedqt.html.[/quote]
Oh, thx!
-
Firstly, the class definition must precede the first to use it, so it pays to place it before the function main.
Secondly, you have unrealized methods open(), save(), quit() - this class does not have a chance to be compiled according to the rules of the C++.
In the third, code that uses advanced Qt (signals and slots, properties, means of introspection), must be pre-processed by the utility MOC. But the build tool (qmake in the first place) do not expect that you will determine the Qt-classes (the heirs of QObject, indicating the Q_OBJECT macro in its private section) in the same file, which is the function main. Problem can be solved in two different ways.
The first method (an unfortunate, example of working code offered below)
Manual call MOC (here we assume that the code is located in a file called main.cpp, all actions are performed at the command prompt in the folder with the file main.cpp, which apart from him nothing)@moc main.cpp -o main.moc@
and include the resulting file to the end of main.cpp:
@#include "main.moc"@
Build the project:
@qmake -project
qmake
make #or nmake for Windows@The second way (most acceptable)
Issue a ruling class in the header file, its implementation in *.cpp file, include the header file in the main file and compile the project.
@#include <QtGui>
class Notepad : public QMainWindow
{
Q_OBJECTpublic:
Notepad();private slots:
void open() {}
void save() {}
void quit() {}private:
QTextEdit *textEdit;QAction *openAction; QAction *saveAction; QAction *exitAction; QMenu *fileMenu;
};
Notepad::Notepad()
{
textEdit = new QTextEdit;
QPushButton *quitButton = new QPushButton("Quit");QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit())); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(textEdit); layout->addWidget(quitButton); QWidget *centralWidget = new QWidget(this); centralWidget->setLayout(layout); setCentralWidget(centralWidget);
}
int main(int argv, char **args)
{
QApplication app(argv, args);Notepad window; window.show(); return app.exec();
}
#include "main.moc"
@ -
If you have only *.h header file you need to write realization in there or split it for 2 files one *.cpp and one *.h. Also Notepad class is inherited QMainWindow in header you wrote : public QMainWindow, and in *,cpp you need to write @Notepad::Notepad(QWidget * parent) : QMainWindow(parent)@ This may be help.