why i am getting below error when i am trying to improve my singleton class ?
-
i have read below part of code to improve the my code so i think i need to write 2 sentence in my code like below :
S(S const&) = delete;
void operator=(S const&) = delete;
Here is an updated C++11 implementation of the Singleton design pattern that is lazy-evaluated, correctly-destroyed, and thread-safe.
class S { public: static S& getInstance() { static S instance; // Guaranteed to be destroyed. // Instantiated on first use. return instance; } private: S() {} // Constructor? (the {} brackets) are needed here. // C++ 03 // ======== // Don't forget to declare these two. You want to make sure they // are inaccessible(especially from outside), otherwise, you may accidentally get copies of // your singleton appearing. S(S const&); // Don't Implement void operator=(S const&); // Don't implement // C++ 11 // ======= // We can use the better technique of deleting the methods // we don't want. public: S(S const&) = delete; void operator=(S const&) = delete; // Note: Scott Meyers mentions in his Effective Modern // C++ book, that deleted functions should generally // be public as it results in better error messages // due to the compilers behavior to check accessibility // before deleted status };
So when i am using that sentence in my code
I am getting below error :
"src\SqliteManager.h", line 22: Error: #319: pure specifier ("= 0") allowed only on virtual functions
SqliteManager(SqliteManager const&) = delete;I have written code like below :
#ifndef SQLMANAGER_H_ #define SQLMANAGER_H_ #include <boomer.h> #include <sqlite/sqlite3.h> #include <vector> #include <string> #include "Header/AFCSTicket.h" //#include "Error.h" using namespace std; namespace app { namespace dbmanager { class SqliteManager { public : SqliteManager(SqliteManager const&) = delete; void operator=(SqliteManager const&) = delete; static SqliteManager* GetDBInstance(); sqlite3* handle; sqlite3_stmt *statement; static sqlite3* getDataBaseHandle(); int prepareStatement(const char *query); int openDatabase(); void closeDatabase(); boomer::transaction::TransactionResult connectToDataBase(std::string fileName, sqlite3* &handle); boomer::transaction::TransactionResult disconnectDataBase(sqlite3* &handle); void closeDatabaseForDBBackup(); private: SqliteManager(); static SqliteManager* iSqlInstanceDB; }; } } #endif
#include "SqliteManager.h" #include <stdio.h> #include <string> using namespace std; using namespace boomer::error; const char* DB_PATH_MAIN = "CONFIG.DB"; namespace app { namespace dbmanager { SqliteManager* SqliteManager::iSqlInstanceDB = NULL; SqliteManager::SqliteManager() { handle = NULL; } SqliteManager* SqliteManager::GetDBInstance() { DBGF_TRACE("SqliteManager::GetDBInstance _heap_max() = %d",_heap_max()); DBGF_TRACE("SqliteManager::GetDBInstance _heap_current() = %d",_heap_current()); if ( iSqlInstanceDB == NULL ) { iSqlInstanceDB = new SqliteManager(); DBGF_TRACE("SqliteManager::GetDBInstance _heap_max() = %d",_heap_max()); DBGF_TRACE("SqliteManager::GetDBInstance _heap_current() = %d",_heap_current()); } else { DBGF_TRACE("SqliteManager::GetDBInstance _heap_max() = %d",_heap_max()); DBGF_TRACE("SqliteManager::GetDBInstance _heap_current() = %d",_heap_current()); } return iSqlInstanceDB; } int SqliteManager::openDatabase() { if(handle != NULL) { DBGF_TRACE("SqliteManager::openDatabase _heap_max() = %d",_heap_max()); DBGF_TRACE("SqliteManager::openDatabase _heap_current() = %d",_heap_current()); return SUCCESS; } int res = sqlite3_open(DB_PATH_MAIN, &handle); if(res != SQLITE_OK) { DBGF_TRACE("SqliteManager::openDatabase _heap_max() = %d",_heap_max()); DBGF_TRACE("SqliteManager::openDatabase _heap_current() = %d",_heap_current()); ERROR_RETURN(0); } else { DBGF_TRACE("openDatabase _heap_max() = %d",_heap_max()); DBGF_TRACE("openDatabase _heap_current() = %d",_heap_current()); if(sqlite3_exec(handle, "PRAGMA journal_mode = WAL;PRAGMA cache_size = 100000;", NULL, 0, NULL) == SQLITE_OK) { DBGF_TRACE("SQLITE QUERY SUCCESS: PRAGMA journal_mode = WAL;PRAGMA cache_size = 100000;"); DBGF_TRACE("openDatabase _heap_max() = %d",_heap_max()); DBGF_TRACE("openDatabase _heap_current() = %d",_heap_current()); return SUCCESS; } else { DBGF_TRACE("SQLITE QUERY FAIL: PRAGMA journal_mode = WAL;PRAGMA cache_size = 100000;"); DBGF_TRACE("openDatabase _heap_max() = %d",_heap_max()); DBGF_TRACE("openDatabase _heap_current() = %d",_heap_current()); ERROR_RETURN(0); } DBGF_TRACE("openDatabase _heap_max() = %d",_heap_max()); DBGF_TRACE("openDatabase _heap_current() = %d",_heap_current()); return SUCCESS; } } int SqliteManager::prepareStatement(const char *query) { if(openDatabase() != SUCCESS) { ERROR_RETURN(0); } statement = NULL; int res = sqlite3_prepare_v2(handle, query, -1, &statement, 0); if(res != SQLITE_OK) { sqlite3_finalize(statement); statement = NULL; ERROR_RETURN(0); }else{ return SUCCESS; } } void SqliteManager::closeDatabase() { DBGF_TRACE("_heap_max() = %d",_heap_max()); DBGF_TRACE("_heap_current() = %d",_heap_current()); // if (handle != NULL) // { // if(sqlite3_close(handle) == SQLITE_OK) // { // DBGF_TRACE("Close Database Success: %s", sqlite3_errmsg(handle)); // handle = NULL; // } // else // LOGF_ERROR("Close Database Error: %s", sqlite3_errmsg(handle)); // } } boomer::transaction::TransactionResult SqliteManager::connectToDataBase(std::string dbFilename , sqlite3* &handle) { int res = 0; res = sqlite3_open(dbFilename.c_str(), &handle); if( res != SQLITE_OK ) { ERROR_RETURN(CONFIG_DB_CONNECT_FAILED); } return boomer::transaction::trOk; } boomer::transaction::TransactionResult SqliteManager::disconnectDataBase(sqlite3* &handle) { long errCode = 0; //Disconnect to DataBase int res = sqlite3_close(handle); if(res) { errCode = CONFIG_DB_DISCONNECT_FAILED; return res; } handle = NULL; return boomer::transaction::trOk; } void SqliteManager::closeDatabaseForDBBackup() { DBGF_TRACE("AFCS:SqliteManager::closeDatabaseForDBBackup()"); if (handle != NULL) { if(sqlite3_close(handle) == SQLITE_OK) { DBGF_TRACE("closeDatabaseForDBBackup::Close Database Success: %s", sqlite3_errmsg(handle)); handle = NULL; } else { //LOGF_ERROR("closeDatabaseForDBBackup::Close Database Error: %s", sqlite3_errmsg(handle)); } } } } }
-
i have read below part of code to improve the my code so i think i need to write 2 sentence in my code like below :
S(S const&) = delete;
void operator=(S const&) = delete;
Here is an updated C++11 implementation of the Singleton design pattern that is lazy-evaluated, correctly-destroyed, and thread-safe.
class S { public: static S& getInstance() { static S instance; // Guaranteed to be destroyed. // Instantiated on first use. return instance; } private: S() {} // Constructor? (the {} brackets) are needed here. // C++ 03 // ======== // Don't forget to declare these two. You want to make sure they // are inaccessible(especially from outside), otherwise, you may accidentally get copies of // your singleton appearing. S(S const&); // Don't Implement void operator=(S const&); // Don't implement // C++ 11 // ======= // We can use the better technique of deleting the methods // we don't want. public: S(S const&) = delete; void operator=(S const&) = delete; // Note: Scott Meyers mentions in his Effective Modern // C++ book, that deleted functions should generally // be public as it results in better error messages // due to the compilers behavior to check accessibility // before deleted status };
So when i am using that sentence in my code
I am getting below error :
"src\SqliteManager.h", line 22: Error: #319: pure specifier ("= 0") allowed only on virtual functions
SqliteManager(SqliteManager const&) = delete;I have written code like below :
#ifndef SQLMANAGER_H_ #define SQLMANAGER_H_ #include <boomer.h> #include <sqlite/sqlite3.h> #include <vector> #include <string> #include "Header/AFCSTicket.h" //#include "Error.h" using namespace std; namespace app { namespace dbmanager { class SqliteManager { public : SqliteManager(SqliteManager const&) = delete; void operator=(SqliteManager const&) = delete; static SqliteManager* GetDBInstance(); sqlite3* handle; sqlite3_stmt *statement; static sqlite3* getDataBaseHandle(); int prepareStatement(const char *query); int openDatabase(); void closeDatabase(); boomer::transaction::TransactionResult connectToDataBase(std::string fileName, sqlite3* &handle); boomer::transaction::TransactionResult disconnectDataBase(sqlite3* &handle); void closeDatabaseForDBBackup(); private: SqliteManager(); static SqliteManager* iSqlInstanceDB; }; } } #endif
#include "SqliteManager.h" #include <stdio.h> #include <string> using namespace std; using namespace boomer::error; const char* DB_PATH_MAIN = "CONFIG.DB"; namespace app { namespace dbmanager { SqliteManager* SqliteManager::iSqlInstanceDB = NULL; SqliteManager::SqliteManager() { handle = NULL; } SqliteManager* SqliteManager::GetDBInstance() { DBGF_TRACE("SqliteManager::GetDBInstance _heap_max() = %d",_heap_max()); DBGF_TRACE("SqliteManager::GetDBInstance _heap_current() = %d",_heap_current()); if ( iSqlInstanceDB == NULL ) { iSqlInstanceDB = new SqliteManager(); DBGF_TRACE("SqliteManager::GetDBInstance _heap_max() = %d",_heap_max()); DBGF_TRACE("SqliteManager::GetDBInstance _heap_current() = %d",_heap_current()); } else { DBGF_TRACE("SqliteManager::GetDBInstance _heap_max() = %d",_heap_max()); DBGF_TRACE("SqliteManager::GetDBInstance _heap_current() = %d",_heap_current()); } return iSqlInstanceDB; } int SqliteManager::openDatabase() { if(handle != NULL) { DBGF_TRACE("SqliteManager::openDatabase _heap_max() = %d",_heap_max()); DBGF_TRACE("SqliteManager::openDatabase _heap_current() = %d",_heap_current()); return SUCCESS; } int res = sqlite3_open(DB_PATH_MAIN, &handle); if(res != SQLITE_OK) { DBGF_TRACE("SqliteManager::openDatabase _heap_max() = %d",_heap_max()); DBGF_TRACE("SqliteManager::openDatabase _heap_current() = %d",_heap_current()); ERROR_RETURN(0); } else { DBGF_TRACE("openDatabase _heap_max() = %d",_heap_max()); DBGF_TRACE("openDatabase _heap_current() = %d",_heap_current()); if(sqlite3_exec(handle, "PRAGMA journal_mode = WAL;PRAGMA cache_size = 100000;", NULL, 0, NULL) == SQLITE_OK) { DBGF_TRACE("SQLITE QUERY SUCCESS: PRAGMA journal_mode = WAL;PRAGMA cache_size = 100000;"); DBGF_TRACE("openDatabase _heap_max() = %d",_heap_max()); DBGF_TRACE("openDatabase _heap_current() = %d",_heap_current()); return SUCCESS; } else { DBGF_TRACE("SQLITE QUERY FAIL: PRAGMA journal_mode = WAL;PRAGMA cache_size = 100000;"); DBGF_TRACE("openDatabase _heap_max() = %d",_heap_max()); DBGF_TRACE("openDatabase _heap_current() = %d",_heap_current()); ERROR_RETURN(0); } DBGF_TRACE("openDatabase _heap_max() = %d",_heap_max()); DBGF_TRACE("openDatabase _heap_current() = %d",_heap_current()); return SUCCESS; } } int SqliteManager::prepareStatement(const char *query) { if(openDatabase() != SUCCESS) { ERROR_RETURN(0); } statement = NULL; int res = sqlite3_prepare_v2(handle, query, -1, &statement, 0); if(res != SQLITE_OK) { sqlite3_finalize(statement); statement = NULL; ERROR_RETURN(0); }else{ return SUCCESS; } } void SqliteManager::closeDatabase() { DBGF_TRACE("_heap_max() = %d",_heap_max()); DBGF_TRACE("_heap_current() = %d",_heap_current()); // if (handle != NULL) // { // if(sqlite3_close(handle) == SQLITE_OK) // { // DBGF_TRACE("Close Database Success: %s", sqlite3_errmsg(handle)); // handle = NULL; // } // else // LOGF_ERROR("Close Database Error: %s", sqlite3_errmsg(handle)); // } } boomer::transaction::TransactionResult SqliteManager::connectToDataBase(std::string dbFilename , sqlite3* &handle) { int res = 0; res = sqlite3_open(dbFilename.c_str(), &handle); if( res != SQLITE_OK ) { ERROR_RETURN(CONFIG_DB_CONNECT_FAILED); } return boomer::transaction::trOk; } boomer::transaction::TransactionResult SqliteManager::disconnectDataBase(sqlite3* &handle) { long errCode = 0; //Disconnect to DataBase int res = sqlite3_close(handle); if(res) { errCode = CONFIG_DB_DISCONNECT_FAILED; return res; } handle = NULL; return boomer::transaction::trOk; } void SqliteManager::closeDatabaseForDBBackup() { DBGF_TRACE("AFCS:SqliteManager::closeDatabaseForDBBackup()"); if (handle != NULL) { if(sqlite3_close(handle) == SQLITE_OK) { DBGF_TRACE("closeDatabaseForDBBackup::Close Database Success: %s", sqlite3_errmsg(handle)); handle = NULL; } else { //LOGF_ERROR("closeDatabaseForDBBackup::Close Database Error: %s", sqlite3_errmsg(handle)); } } } } }
@Qt-embedded-developer
what compiler and version of it are you using? -
@Qt-embedded-developer
what compiler and version of it are you using?@raven-worx i am using arm 9 based compiler. the compiler version i am not knowing. I am using embedded OS named verix based oS.
-
Your code you posted does not match the error message. Make sure the correct header is picked up.