static members

Discussion in 'C++' started by daniel, Jul 18, 2008.

  1. daniel

    daniel Guest

    Hello ,

    I have the following code , which implements the singleton pattern:

    class Singleton{
    private:
    static Singleton* uniqueInstance;
    //other useful instance variables here

    Singleton(){
    cout<<"Object created!"<<endl;
    }

    public:
    static Singleton* getInstance(){
    if(uniqueInstance == NULL)
    uniqueInstance = new Singleton();
    return uniqueInstance;
    }
    };

    int main(){
    Singleton::getInstance();
    return 0;
    }

    But when i try to compile it , i get the following error:

    dan@sea:~/school/dp$ g++ -o singleton singleton.cpp
    /tmp/ccHL1rvi.o: In function `Singleton::getInstance()':
    singleton.cpp:
    (.text._ZN9Singleton11getInstanceEv[Singleton::getInstance()]+0x8):
    undefined reference to `Singleton::uniqueInstance'
    singleton.cpp:
    (.text._ZN9Singleton11getInstanceEv[Singleton::getInstance()]+0x2e):
    undefined reference to `Singleton::uniqueInstance'
    singleton.cpp:
    (.text._ZN9Singleton11getInstanceEv[Singleton::getInstance()]+0x54):
    undefined reference to `Singleton::uniqueInstance'
    collect2: ld returned 1 exit status

    Can you enlighten me :)

    Thanks,
    Daniel.
    daniel, Jul 18, 2008
    #1
    1. Advertising

  2. daniel

    Lionel B Guest

    On Fri, 18 Jul 2008 04:09:03 -0700, daniel wrote:

    > Hello ,
    >
    > I have the following code , which implements the singleton pattern:
    >
    > class Singleton{
    > private:
    > static Singleton* uniqueInstance;
    > //other useful instance variables here
    >
    > Singleton(){
    > cout<<"Object created!"<<endl;
    > }
    >
    > public:
    > static Singleton* getInstance(){
    > if(uniqueInstance == NULL)
    > uniqueInstance = new Singleton();
    > return uniqueInstance;
    > }
    > };
    >
    > int main(){
    > Singleton::getInstance();
    > return 0;
    > }
    >
    > But when i try to compile it , i get the following error:
    >
    > dan@sea:~/school/dp$ g++ -o singleton singleton.cpp /tmp/ccHL1rvi.o: In
    > function `Singleton::getInstance()': singleton.cpp:
    > (.text._ZN9Singleton11getInstanceEv[Singleton::getInstance()]+0x8):
    > undefined reference to `Singleton::uniqueInstance' singleton.cpp:
    > (.text._ZN9Singleton11getInstanceEv[Singleton::getInstance()]+0x2e):
    > undefined reference to `Singleton::uniqueInstance' singleton.cpp:
    > (.text._ZN9Singleton11getInstanceEv[Singleton::getInstance()]+0x54):
    > undefined reference to `Singleton::uniqueInstance' collect2: ld returned
    > 1 exit status
    >
    > Can you enlighten me :)


    The linker is telling you that it can't find a definition for
    Singleton::uniqueInstance. You have declared it, but my guess is that you
    haven't *defined* it; static member variables must be defined in some
    compilation unit. See:

    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.11


    --
    Lionel B
    Lionel B, Jul 18, 2008
    #2
    1. Advertising

  3. daniel a écrit :
    > I have the following code , which implements the singleton pattern:
    >
    > class Singleton{
    > private:
    > static Singleton* uniqueInstance;
    > //other useful instance variables here
    >
    > Singleton(){
    > cout<<"Object created!"<<endl;
    > }
    >
    > public:
    > static Singleton* getInstance(){
    > if(uniqueInstance == NULL)
    > uniqueInstance = new Singleton();
    > return uniqueInstance;
    > }
    > };


    You must instantiate Singleton::uniqueInstance.
    Add the following line somewhere in a cpp:

    Singleton* Singleton::uniqueInstance;


    >
    > int main(){
    > Singleton::getInstance();
    > return 0;
    > }
    >
    > But when i try to compile it , i get the following error:
    >
    > dan@sea:~/school/dp$ g++ -o singleton singleton.cpp
    > /tmp/ccHL1rvi.o: In function `Singleton::getInstance()':
    > singleton.cpp:
    > (.text._ZN9Singleton11getInstanceEv[Singleton::getInstance()]+0x8):
    > undefined reference to `Singleton::uniqueInstance'

    [snip]

    --
    Michael
    Michael DOUBEZ, Jul 18, 2008
    #3
  4. daniel

    daniel Guest

    On Jul 18, 2:18 pm, Lionel B <> wrote:
    > On Fri, 18 Jul 2008 04:09:03 -0700, daniel wrote:
    > > Hello ,

    >
    > > I have the following code , which implements the singleton pattern:

    >
    > > class Singleton{
    > > private:
    > >     static Singleton* uniqueInstance;
    > >     //other useful instance variables here

    >
    > >     Singleton(){
    > >         cout<<"Object created!"<<endl;
    > >     }

    >
    > > public:
    > >     static Singleton* getInstance(){
    > >         if(uniqueInstance == NULL)
    > >             uniqueInstance = new Singleton();
    > >         return uniqueInstance;
    > >     }
    > > };

    >
    > > int main(){
    > >     Singleton::getInstance();
    > >     return 0;
    > > }

    >
    > > But when i try to compile it , i get the following error:

    >
    > > dan@sea:~/school/dp$ g++  -o singleton singleton.cpp /tmp/ccHL1rvi.o: In
    > > function `Singleton::getInstance()': singleton.cpp:
    > > (.text._ZN9Singleton11getInstanceEv[Singleton::getInstance()]+0x8):
    > > undefined reference to `Singleton::uniqueInstance' singleton.cpp:
    > > (.text._ZN9Singleton11getInstanceEv[Singleton::getInstance()]+0x2e):
    > > undefined reference to `Singleton::uniqueInstance' singleton.cpp:
    > > (.text._ZN9Singleton11getInstanceEv[Singleton::getInstance()]+0x54):
    > > undefined reference to `Singleton::uniqueInstance' collect2: ld returned
    > > 1 exit status

    >
    > > Can you enlighten me :)

    >
    > The linker is telling you that it can't find a definition for
    > Singleton::uniqueInstance. You have declared it, but my guess is that you
    > haven't *defined* it; static member variables must be defined in some
    > compilation unit. See:
    >
    > http://www.parashift.com/c -faq-lite/ctors.html#faq-10.11
    >
    > --
    > Lionel B


    got it , thanks a lot
    daniel, Jul 18, 2008
    #4
  5. daniel

    Guest

    Hi daniel,

    while writing the singleton class you can avoid using pointer and pass
    your class as reference
    that will be more effective.

    Rewriting ur code :

    class Singleton{

    private:
    static Singleton uniqueInstance;
    //other useful instance variables here

    private:
    Singleton();
    ~Singleton();

    public:
    static Singleton& getInstance(){
    return uniqueInstance;
    }

    };


    int main(){
    Singleton::getInstance();
    return 0;
    }

    +suresh
    , Jul 18, 2008
    #5
  6. On Jul 18, 9:06 am, wrote:
    > Hi daniel,
    >
    > while writing the singleton class you can avoid using pointer and pass
    > your class as reference
    > that will be more effective.
    >
    > Rewriting ur code :
    >
    > class Singleton{
    >
    > private:
    >     static Singleton uniqueInstance;
    >     //other useful instance variables here
    >
    > private:
    >    Singleton();
    >    ~Singleton();
    >
    > public:
    >     static Singleton& getInstance(){
    >         return uniqueInstance;
    >     }
    >
    > };
    >
    > int main(){
    >     Singleton::getInstance();
    >     return 0;
    >
    > }
    >
    > +suresh


    Not going to work, for you forgot to define a uniqueInstance.
    puzzlecracker, Jul 18, 2008
    #6
  7. daniel

    Guest


    >
    > Not going to work, for you forgot to define a uniqueInstance.- Hide quoted text -
    >
    > - Show quoted text -


    Hi Cracker,

    Can i know the reason ?
    , Jul 18, 2008
    #7
  8. daniel

    Lionel B Guest

    On Fri, 18 Jul 2008 06:46:11 -0700, vsuresh.cs wrote:

    >> Not going to work, for you forgot to define a uniqueInstance.

    >
    > Hi Cracker,
    >
    > Can i know the reason ?


    For the same reason the OP's code didn't compile in the first place. Go
    back and re-read this thread from the beginning.

    --
    Lionel B
    Lionel B, Jul 18, 2008
    #8
  9. daniel

    Guest

    On Jul 18, 2:06 pm, wrote:
    > class Singleton{
    >
    > private:
    >     static Singleton uniqueInstance;
    >     //other useful instance variables here
    >
    > private:
    >    Singleton();
    >    ~Singleton();
    >
    > public:
    >     static Singleton& getInstance(){
    >         return uniqueInstance;
    >     }
    >
    > };


    Instead of private static member for the uniqueInstance try static
    variable in the scope of the getInstance() function.

    static Singleton& getInstance(){
    static Singleton uniqueInstance;
    return uniqueInstance;
    }

    The singleton will be constructed on the first access.

    Kind regards,
    Vladimir
    , Jul 18, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben
    Replies:
    3
    Views:
    410
  2. SaravanaKumar
    Replies:
    6
    Views:
    9,358
    Tony Morris
    Oct 19, 2004
  3. JFCM
    Replies:
    4
    Views:
    5,719
  4. Rakesh Sinha
    Replies:
    4
    Views:
    1,838
    Rakesh Sinha
    Jan 13, 2005
  5. Hicham Mouline
    Replies:
    5
    Views:
    2,341
    James Kanze
    Dec 19, 2008
Loading...

Share This Page