2010-06-10 75 views
3

我有C++的經驗,但我從來沒有真正使用過Qt。我試圖連接到一個SQLite數據庫,所以我找到了一個教程here,並正在與此。在QtCreator IDE中,我去了Add New - > C++ Class,並在頭文件中粘貼了頭文件中的頭文件,並在該頭文件中粘貼了源代碼.cpp文件。我的main.cpp看起來是這樣的:Qt 101:爲什麼我不能使用這個課程?

#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()' 

任何人都可以點我在正確的方向?我知道「copypaste」代碼不好,我只想看看能否使數據庫連接正常工作,並且我認爲這樣做很簡單......感謝您的幫助。

回答

7

變化將DatabaseManager行:

DatabaseManager db; 

你聲明稱爲db本地函數不帶參數,當你提供()返回DatabaseManager對象;

+0

嘗試此更早,結束於: main.cpp:11:未定義的引用'DatabaseManager :: DatabaseManager(QObject *)' – Joseph 2010-06-10 03:24:56

+0

這看起來像一個鏈接錯誤。確保你在QT庫中鏈接。 – 2010-06-10 03:32:20

+0

它不是一個鏈接錯誤,它說沒有爲這些參數定義的構造函數。 – Akusete 2010-06-10 03:38:00

相關問題