error: no matching function or call to 'MainWindow::~MainWindow()'
无法运行应用 - QtSql
。我无法用谷歌的方式来解决这个问题。我已经包含了我的项目文件中的代码 - mainwindow.cpp
,main.cpp
和mainwindow.h
。我还在.pro
中包含了sql(QT + = core gui sql)。
代码:
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;
}
编译输出:
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
}
答案 0 :(得分:1)
您忘记在析构函数定义之前关闭括号:
//...
db.setPassword(QString("PASSWORD"));
db.open();
} //close a bracket
MainWindow::~MainWindow() // ERROR
{
//...