Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. why i am getting below error when i am trying to improve my singleton class ?
Forum Updated to NodeBB v4.3 + New Features

why i am getting below error when i am trying to improve my singleton class ?

Scheduled Pinned Locked Moved Unsolved C++ Gurus
4 Posts 3 Posters 469 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by Qt embedded developer
    #1

    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));
    		}
    	}
    }
    
    }
    
    }
    
    
    raven-worxR 1 Reply Last reply
    0
    • Q Qt embedded developer

      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));
      		}
      	}
      }
      
      }
      
      }
      
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Qt-embedded-developer
      what compiler and version of it are you using?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      Q 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @Qt-embedded-developer
        what compiler and version of it are you using?

        Q Offline
        Q Offline
        Qt embedded developer
        wrote on last edited by Qt embedded developer
        #3

        @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.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Your code you posted does not match the error message. Make sure the correct header is picked up.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved