问题描述:我原本想把createConnection函数封装在类中,课是当我定义变量new studentTable 时却出项了下列问题,导致我无法把createConnection函数封装在类中,问题我已经注释在语句上了,求高人指点,要如何才能把createConnection函数封装在类中 ????
- C/C++ code
#include <QtGui>#include <QtSql>#include <iostream>#include <QTextCodec>using namespace std;#include "student_table.h"bool createConnection(){ QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); db.setDatabaseName("edu_sys"); db.setUserName("root"); db.setPassword("123456"); if (!db.open()) { QMessageBox::critical(0,QObject::tr("Database Error"), db.lastError().text()); return false; } return true;}int main(int argc, char *argv[]){ QApplication app(argc, argv); QTextCodec *codec = QTextCodec::codecForName("GBK"); QTextCodec::setCodecForLocale(codec); QTextCodec::setCodecForCStrings(codec); QTextCodec::setCodecForTr(codec); //StudentTable *studentTable = new StudentTable;//为什么在这边定义就无法连接到数据库,而在下面定义却可以??? if (!createConnection()) { cout<<"error"; return 1; } StudentTable *studentTable = new StudentTable; studentTable->show(); studentTable->resize(900,200); return app.exec();}
------解决方案--------------------
好好看看书