Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static members

Reply
Thread Tools

static members

 
 
daniel
Guest
Posts: n/a
 
      07-18-2008
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()]+0x:
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.

 
Reply With Quote
 
 
 
 
Lionel B
Guest
Posts: n/a
 
      07-18-2008
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()]+0x:
> 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-lit...html#faq-10.11


--
Lionel B
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      07-18-2008
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()]+0x:
> undefined reference to `Singleton::uniqueInstance'

[snip]

--
Michael
 
Reply With Quote
 
daniel
Guest
Posts: n/a
 
      07-18-2008
On Jul 18, 2:18*pm, Lionel B <m...@privacy.net> 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()]+0x:
> > 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-lit...html#faq-10.11
>
> --
> Lionel B


got it , thanks a lot
 
Reply With Quote
 
vsuresh.cs@gmail.com
Guest
Posts: n/a
 
      07-18-2008

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
 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      07-18-2008
On Jul 18, 9:06*am, vsuresh...@gmail.com 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.
 
Reply With Quote
 
vsuresh.cs@gmail.com
Guest
Posts: n/a
 
      07-18-2008

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


Hi Cracker,

Can i know the reason ?
 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      07-18-2008
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
 
Reply With Quote
 
vova777@gmail.com
Guest
Posts: n/a
 
      07-18-2008
On Jul 18, 2:06*pm, vsuresh...@gmail.com 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread safety problems with function scope static variables vs class static private members Hicham Mouline C++ 5 12-19-2008 08:10 PM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
About static const members appearing in another static const definitions Rakesh Sinha C++ 4 01-13-2005 08:11 AM
Instantiating a static class( Class with all static members - methods and variables) SaravanaKumar Java 6 10-19-2004 08:20 AM
Static classes with static members Ben ASP .Net 3 06-01-2004 07:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57