Bonjour. Je souhaite réaliser une classe Singleton pour pouvoir me connecter à une BDD, mais je rame un peu !
Voici l'erreur que j'obtiens à la compilation :
DBConnection.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 DBConnection.h: In member function `DBConnection& DBConnection::operator=(const DBConnection&)': DBConnection.h:20: warning: no return statement in function returning non-void DBConnection.cpp: In constructor `DBConnection::DBConnection()': DBConnection.cpp:9: error: `class QSqlDatabase::addDatabase' is not a type DBConnection.cpp: At global scope: DBConnection.cpp:34: error: ISO C++ forbids declaration of `get_db' with no type DBConnection.cpp:34: error: prototype for `int DBConnection::get_db()' does not match any in class `DBConnection' DBConnection.h:12: error: candidate is: static DBConnection DBConnection::get_db () DBConnection.cpp:34: error: `int DBConnection::get_db()' and `static DBConnectio n DBConnection::get_db()' cannot be overloaded DBConnection.cpp: In member function `int DBConnection::get_db()': DBConnection.cpp:35: error: cannot convert `QSqlDatabase' to `int' in return
DBConnection.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 #ifndef DEF_DBCONNECTION #define DEF_DBCONNECTION #include <QSqlDatabase> #include <iostream> class DBConnection { public: static DBConnection* Get(); static void Kill(); static DBConnection get_db(); private: static DBConnection* m_instance; DBConnection(); ~DBConnection(); DBConnection& operator= (const DBConnection&){} DBConnection (const DBConnection&){} QSqlDatabase db; bool check_con; }; #endif
Et le morceau de code que j'utilise pour créer ma connection :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 #include "DBConnection.h" using namespace std; DBConnection* DBConnection::m_instance=0; DBConnection::DBConnection() { db = new QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); db.setDatabaseName("bob"); db.setUserName("truc"); db.setPassword("muche"); } DBConnection::~DBConnection(){} DBConnection* DBConnection::Get() { if(m_instance==0) { m_instance=new DBConnection(); } return m_instance; } void DBConnection::Kill() { delete m_instance; m_instance=0; } DBConnection::get_db() { return db; }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 DBConnection* con_DB=DBConnection::Get(); db = con_DB->get_db(); if( db.open()) { m_TexteInformation->setText("réussi !"); } else { m_TexteInformation->setText(":(");}
Voilà... merci d'avance.
Partager