Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Help with singleton

Help with singleton

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 1.7k 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.
  • E Offline
    E Offline
    Exotic_Devel
    wrote on last edited by
    #1

    In my singleton class, I am getting errors:

    -/home/matheus/Dropbox/Projetos/Freedom/TecTracker/Core/dataaccess/dataaccess.cpp:28: error: ISO C++ forbids declaration of 'instance' with no type [-fpermissive]
    DataAccess::instance()
    ^-

    /home/matheus/Dropbox/Projetos/Freedom/TecTracker/Core/dataaccess/dataaccess.cpp:28: error: prototype for 'int DataAccess::instance()' does not match any in class 'DataAccess'
    DataAccess::instance()
    ^

    /home/matheus/Dropbox/Projetos/Freedom/TecTracker/Core/dataaccess/dataaccess.cpp:21: In file included from ../../../TecTracker/Core/dataaccess/dataaccess.cpp:21:0:

    /home/matheus/Dropbox/Projetos/Freedom/TecTracker/Core/dataaccess/dataaccess.hpp:29: error: candidate is: static DataAccess* DataAccess::instance()
    static DataAccess* instance(); /* <= Devolve um objeto do tipo DataAccess*/
    ^

    Below the code:

    @/Classe singleton/

    #ifndef DATAACCESS_HPP
    #define DATAACCESS_HPP

    class DataAccess
    {
    public:
    static DataAccess* instance(); /* <= Devolve um objeto do tipo DataAccess*/

    private:
    DataAccess();
    DataAccess(const DataAccess&);
    DataAccess& operator=(const DataAccess&);
    static DataAccess* dataaccess; /* <= Objeto DataAccess*/
    };

    #endif // DATAACCESS_HPP@

    @#include "dataaccess.hpp"

    DataAccess::DataAccess()
    {
    }

    DataAccess::instance()
    {

    if(!dataaccess)
    dataaccess = new DataAccess;

    return dataaccess;
    }
    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      I think you need to change:
      @DataAccess::instance()
      {

      if(!dataaccess)
      dataaccess = new DataAccess;

      return dataaccess;
      }@

      To:
      @DataAccess *DataAccess::instance()
      {

      if(!dataaccess)
      dataaccess = new DataAccess;

      return dataaccess;
      }@

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vezprog
        wrote on last edited by
        #3

        I like to use the lazy singleton method when using it for logging, then you don't have to worry about the pointer allocation and memory cleanup

        class definition in .h
        @
        class Singleton
        {
        public:
        static Singleton& instance();

        private:
        Singleton();
        Singleton& operator = (const Singleton&);
        ~Singleton();

         static Singleton mInstance;
        

        }
        @

        in .cpp
        @
        Singleton Singleton::mInstance;

        Singleton& Singleton::instance()
        {
        return mInstance;
        }

        Singleton::~Singleton()
        {
        }
        @

        Its all a matter of preference though.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          Exotic_Devel
          wrote on last edited by
          #4

          Thank boys

          dvez43

          I do not understand your code. What line you instantiate the object? And where do you test if the object already exists? I do not recognize this kind of singleton.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            arsinte_andrei
            wrote on last edited by
            #5

            @ /Classe singleton/

            #ifndef DATAACCESS_HPP
            #define DATAACCESS_HPP
             
            class DataAccess
            {
            public:
              static DataAccess *instance(); 
            

            static uninit();
            ~DataAccess();

            private:
              DataAccess();
              DataAccess(const DataAccess&);
              DataAccess& operator=(const DataAccess&);
              static DataAccess *dataaccess;
            };
             
            #endif // DATAACCESS_HPP
            

            @

            @ #include "dataaccess.hpp"
            DataAccess DataAccess::dataaccess = NULL;

            DataAccess::DataAccess()
            {
            }
             
            DataAccess::instance()
            {
             
              if(dataaccess == NULL){
                dataaccess = new DataAccess;
            

            }

              return dataaccess;
            }
            

            DataAccess::uninit(){
            if (dataaccess !=NULL){
            delete dataaccess;
            dataaccess = NULL
            }

            DataAccess::~DataAccess(){
            delete dataaccess;
            }
            @

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vezprog
              wrote on last edited by
              #6

              You do not need to instantiate the object if it is a static object as a member variable. Singleton Singleton::mInstance; in the cpp makes sure the object is created... everything is allocated on the stack.

              The only to access the object publicly is through the instance function which returns the public member by reference.

              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