Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem in creating singleton class

problem in creating singleton class

Scheduled Pinned Locked Moved C / C++ / MFC
help
3 Posts 2 Posters 0 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.
  • S Offline
    S Offline
    Shraddha Gautam
    wrote on last edited by
    #1

    HELLO, We have tried creating singleton class. the code is as follows class singleton { private: static singleton * single; singleton() { } public: static singleton* getinstance(); }; singleton* singleton :: getinstance() { if(NULL==single) single = new singleton(); return single; } main() { singleton *obj_singelton; obj_singleton = singleton::getinstance(); } for this we are getting the following linker error undefined symbol singleton::single.

    P 1 Reply Last reply
    0
    • S Shraddha Gautam

      HELLO, We have tried creating singleton class. the code is as follows class singleton { private: static singleton * single; singleton() { } public: static singleton* getinstance(); }; singleton* singleton :: getinstance() { if(NULL==single) single = new singleton(); return single; } main() { singleton *obj_singelton; obj_singleton = singleton::getinstance(); } for this we are getting the following linker error undefined symbol singleton::single.

      P Offline
      P Offline
      Parthi_Appu
      wrote on last edited by
      #2

      Shraddha Gautam wrote:

      for this we are getting the following linker error undefined symbol singleton::single

      You have to initialize the static member outside the class declaration as,

      singleton* singleton::single = NULL;

      And if your application is multithreaded then you have to use some locking mechanism or if you sure that this singleton instance will be needed from the application start time then modify the initilization as,

      singleton* singleton::single = new singleton;


      Do your Duty and Don't expect the Result

      S 1 Reply Last reply
      0
      • P Parthi_Appu

        Shraddha Gautam wrote:

        for this we are getting the following linker error undefined symbol singleton::single

        You have to initialize the static member outside the class declaration as,

        singleton* singleton::single = NULL;

        And if your application is multithreaded then you have to use some locking mechanism or if you sure that this singleton instance will be needed from the application start time then modify the initilization as,

        singleton* singleton::single = new singleton;


        Do your Duty and Don't expect the Result

        S Offline
        S Offline
        Shraddha Gautam
        wrote on last edited by
        #3

        Thanks. We tried using that but it is creating two objects. Our application is not a multithreading.plz let know what has to be done.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups