Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ qt-mysql连接错误_C++_Linux_Qt_Centos - Fatal编程技术网

C++ qt-mysql连接错误

C++ qt-mysql连接错误,c++,linux,qt,centos,C++,Linux,Qt,Centos,我正在测试QT上的数据库连接,但我发现了这个错误 [root@server env]# make g++ -c -m64 -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I../../../li

我正在测试QT上的数据库连接,但我发现了这个错误

[root@server env]# make
g++ -c -m64 -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I../../../lib64/qt4/mkspecs/linux-g++-64 -I. -I../../../include/QtCore -I../../../include/QtGui -I../../../include -I. -I. -o test.o test.cpp
test.cpp: In function ‘int main()’:
test.cpp:20: error: cannot convert ‘QSqlDatabase’ to ‘int’ in return
test.cpp: At global scope:
test.cpp:23: error: ‘Database’ has not been declared
make: *** [test.o] Error 1
代码如下:

#include <stdlib.h>
#include <iostream>
#include "/usr/include/mysql/mysql.h"
#include <QDebug>
#include <QtSql/QtSql>

using namespace std;

int main() {

    cout << "SHELL = " << getenv("SHELL") << endl;
    cout << "SHELL = " << getenv("TERM") << endl;

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("xxx.xxxx.xxxx.xxx");
    db.setDatabaseName("SAMS");
    db.setUserName("xxxxxxxx");
    db.setPassword("xxxxxxxxxx");
    if (!db.open()) qDebug() << "Failed to connect to mysql as user testuser";
    return db;
}

void Database::closeDb(){
    QSqlDatabase::removeDatabase("QMYSQL");
}

int main()
函数中返回
QSqlDatabase
,这是无效的,因为
QSqlDatabase
不是
int
。你为什么还它?例如,如果希望返回类型描述成功,请使用
-1
0

至于第二个错误,您没有定义任何名为
Database
的类,因此无法定义方法
Database::closeDb()
。而是创建一个函数来执行您想要的操作:

void closeDb() {
  QSqlDatabase::removeDatabase("QMYSQL");
}

但是我看不出
closeDb()
在您不使用它的情况下的作用。

这是因为您仍然需要
int main()
函数,因为它是程序的入口点。未定义的引用错误意味着编译器无法找到必要的头/类信息。您需要确保您拥有用于
QSqlDatabase
等的代码。请注意,这在您发布的第一个代码段中不是问题,因此您可能已经更改了compile语句。
#include <stdlib.h>
#include <iostream>
#include "/usr/include/mysql/mysql.h"
#include <QDebug>
#include <QtSql/QtSql>

using namespace std;

void conn() {

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("xxxxxxxxxxx");
    db.setDatabaseName("SAMS");
    db.setUserName("xxxxxxxx");
    db.setPassword("xxxxxxxx");
    if (!db.open()) {
        qDebug() << "Failed to connect to mysql as user testuser";
    }
    else {
        qDebug() << "hello";
    }
}

void closeDb() {
    QSqlDatabase::removeDatabase("QMYSQL");
}

int main() {

    cout << "SHELL = " << getenv("SHELL") << endl;
    cout << "SHELL = " << getenv("TERM") << endl;
    conn();
    return 0;

}
QT += core gui sql
void closeDb() {
  QSqlDatabase::removeDatabase("QMYSQL");
}