我有使用 C++ 的经验,但我以前从未真正使用过 Qt。我正在尝试连接到 SQLite 数据库,所以我找到了一个教程 here我会这样做的。在 QtCreator IDE 中,我转到 Add New --> C++ Class 并在 header 中粘贴了来自该链接的 header 的头文件,并在 .cpp 文件中粘贴了源代码。我的 main.cpp 看起来像这样:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "databasemanager.h"
#include <qlabel.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
DatabaseManager db();
QLabel hello("nothing...");
if(db.openDB()){ // Line 13
hello.setText("Win!");
}
else{
hello.setText("Lame!");
}
hello.resize(100, 30);
hello.show();
return a.exec();
}
我收到这个错误:
main.cpp:13: error: request for member 'openDB' in 'db', which is of non-class type 'DatabaseManager()'
谁能指出我正确的方向?我知道“复制粘贴”代码不好,我只是想看看我是否能让数据库连接正常工作,我认为这样的事情会很简单……感谢您的帮助。
最佳答案
将 DatabaseManager 行更改为:
DatabaseManager db;
您正在声明一个名为 db
的本地函数,它不接受任何参数并在您提供 ()
时返回一个 DatabaseManager
对象;
关于c++ - Qt 101 : Why can't I use this class?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3011421/