c++ - Qt 101 : Why can't I use this class?

标签 c++ qt sqlite class

我有使用 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/

相关文章:

c++ - as_ldt_init 初始化失败——C++ 客户端

c++ - std::exp(-100.0f) == NaN?

c++ - 如何制作 Web 浏览器工具栏?

c++ - QCombobox::setView 在 Windows 7 上崩溃应用程序

mysql - 根据另一个表从一个表中选择条目

database - 使用 vb.net 打开 sqlite "Temporary"数据库的确切语法是什么

c++ - 初始化程序引用列表的替代方法

qt - 无法使用按钮 : QML 关闭窗口

c++ - 在图形 View 中剪切复制粘贴

java - 来自 CursorWindow 的 java.lang.IllegalStateException : Couldn't read row 0, col -1