2
error: no matching function or call to 'MainWindow::~MainWindow()'

无法运行应用程序 - QtSql。我在谷歌上找不到解决这个问题的方法。我已经包含了我的项目文件中的代码-mainwindow.cpp和. 而且我还在(QT += core gui sql)中包含了sql。main.cppmainwindow.h.pro

代码:

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

主.cpp:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

主窗口.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
 }
4

1 回答 1

1

您忘记在定义析构函数之前关闭一个括号:

//...
    db.setPassword(QString("PASSWORD"));

    db.open();
} //close a bracket 

MainWindow::~MainWindow()   // ERROR
{
//...
于 2013-06-30T15:56:59.700 回答