static members

D

daniel

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.
 
L

Lionel B

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
 
M

Michael DOUBEZ

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]
 
D

daniel

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

got it , thanks a lot
 
V

vsuresh.cs

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
 
P

puzzlecracker

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.
 
L

Lionel B

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.
 
V

vova777

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top