You forgot to close a bracket before the definition of the destructor:
//...
db.setPassword(QString("PASSWORD"));
db.open();
} //close a bracket
MainWindow::~MainWindow() // ERROR
{
//...
Pregunta
error: no matching function or call to 'MainWindow::~MainWindow()'
Cannot run an app - QtSql
. I couldn't find in google way to solve this problem. I have included code from my project files - mainwindow.cpp
, main.cpp
and mainwindow.h
. And I have also included sql in .pro
(QT += core gui sql).
Code:
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtDebug>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlTableModel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
virtual ~MainWindow();
private:
Ui::MainWindow *ui;
QSqlDatabase db;
QSqlTableModel *model;
};
#endif // MAINWINDOW_H
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
db = QSqlDatabase::addDatabase(QString("QMYSQL"));
db.setHostName(QString("LOCALHOST\\MYSQL"));
db.setDatabaseName(QString("testdb"));
db.setUserName(QString("ROOT"));
db.setPassword(QString("PASSWORD"));
db.open();
MainWindow::~MainWindow() // ERROR
{
db.close();
delete ui;
}
Compile output:
16:34:42: Running steps for project TisbiCourseWork...
16:34:42: Configuration unchanged, skipping qmake step.
16:34:42: Starting: "/usr/bin/make"
g++ -c -m64 -pipe -g -Wall -W -D_REENTRANT -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I../TisbiCourseWork -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtSql -I/usr/include/qt4 -I. -I. -I../TisbiCourseWork -I. -o main.o ../TisbiCourseWork/main.cpp
g++ -c -m64 -pipe -g -Wall -W -D_REENTRANT -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I../TisbiCourseWork -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtSql -I/usr/include/qt4 -I. -I. -I../TisbiCourseWork -I. -o mainwindow.o ../TisbiCourseWork/mainwindow.cpp
../TisbiCourseWork/mainwindow.cpp: In constructor 'MainWindow::MainWindow(QWidget*)':
../TisbiCourseWork/mainwindow.cpp:18:25: error: no matching function for call to 'MainWindow::~MainWindow()'
MainWindow::~MainWindow()
^
../TisbiCourseWork/mainwindow.cpp:18:25: note: candidate is:
In file included from ../TisbiCourseWork/mainwindow.cpp:1:0:
../TisbiCourseWork/mainwindow.h:20:13: note: virtual MainWindow::~MainWindow()
virtual ~MainWindow();
^
../TisbiCourseWork/mainwindow.h:20:13: note: candidate expects 1 argument, 0 provided
../TisbiCourseWork/mainwindow.cpp:19:1: error: expected ';' before '{' token
{
^
../TisbiCourseWork/mainwindow.cpp:22:1: error: expected '}' at end of input
}
Solución
You forgot to close a bracket before the definition of the destructor:
//...
db.setPassword(QString("PASSWORD"));
db.open();
} //close a bracket
MainWindow::~MainWindow() // ERROR
{
//...