Undefined reference to Singleton::instance - c++

I am trying to code a Singleton. When I compile it I get many undefined errors, like instance and mutex_ in the getSingleton()
undefined reference to 'Singleton::instance'
undefined reference to 'Singleton::mutex_'
#include<iostream>
#include<string>
#include<mutex>
using namespace std;
class Singleton{
public:
static Singleton* getSingleton(){
if(instance == NULL){
mutex_.lock();
if(instance == NULL){
instance = new Singleton();
}
mutex_.unlock();
}
return instance;
}
private:
Singleton(){}
Singleton& operator =(const Singleton& ){}
static Singleton *instance;
static mutex mutex_;
};
int main(){
Singleton* singleton = Singleton::getSingleton();
return 0;
}

You should place define this static fields to *.cpp file like there
class Singleton{
public:
static Singleton* getSingleton(){
if(instance == NULL){
mutex_.lock();
if(instance == NULL){
instance = new Singleton();
}
mutex_.unlock();
}
return instance;
}
private:
Singleton(){}
Singleton& operator =(const Singleton& ){}
static Singleton *instance;
static mutex mutex_;
};
mutex Singleton::mutex_;
Singleton * Singleton::instance;
You can remove the mutex if you use singleton myers and c++11:
class Singleton {
public:
static Singleton& Instance() {
static Singleton S;
return S;
}
private:
Singleton();
~Singleton();
};

Related

definition of static variable inside a class member function

I am learning singleton design pattern and came across this piece of code
class Singleton
{
private:
static Singleton * pinstance_;
static std::mutex mutex_;
protected:
static Singleton *GetInstance(const std::string& value);
};
Singleton* Singleton::pinstance_{nullptr}; //can this line be removed?
std::mutex Singleton::mutex_; //can this line be removed?
Singleton *Singleton::GetInstance(const std::string& value)
{
std::lock_guard<std::mutex> lock(mutex_);
if (pinstance_ == nullptr)
{
pinstance_ = new Singleton(value);
}
return pinstance_;
}
since pinstance_ and mutex_ are all members of Singleton class, they are accessible by GetInstance method. So my question is: can these two definition lines be removed?
No, they can't be removed but they can be moved to local static variables of the method.
class Singleton
{
protected:
static Singleton *GetInstance(const std::string& value);
};
Singleton *Singleton::GetInstance(const std::string& value)
{
static Singleton* pinstance_{nullptr};
static std::mutex mutex_;
std::lock_guard<std::mutex> lock(mutex_);
if (pinstance_ == nullptr)
{
pinstance_ = new Singleton(value);
}
return pinstance_;
}
Also note, no guards required for initializing static variables. The compiler does it for you. Is local static variable initialization thread-safe in C++11? [duplicate]
Singleton *Singleton::GetInstance(const std::string& value)
{
static Singleton* pinstance_ = new Singleton(value);
return pinstance_;
}

Is this correct program of making a class singleton?

I'm new to programing and I was trying for the program which makes the class singleton..Is this the correct approach for making a class singleton??
#include <iostream>
using namespace std;
class Singleton
{
private:
static bool instanceFlag;
static Singleton *single;
public:
static Singleton* getInstance();
void method();
~Singleton()
{
instanceFlag = false;
}
};
bool Singleton::instanceFlag = false;
Singleton* Singleton::single = NULL;
Singleton* Singleton::getInstance()
{
if(! instanceFlag)
{
single = new Singleton();
instanceFlag = true;
return single;
}
else
{
return single;
}
}
void Singleton::method()
{
cout << "Method of the singleton class";
}
int main()
{
Singleton *sc1,*sc2;
sc1 = Singleton::getInstance();
sc1->method();
sc2=Singleton::getInstance();
sc2->method();
return 0;
}
Is this the correct way of making class singleton??
You are over-complicating things. Try the Scott Meyers singleton:
struct SingletonClass {
// instance() function return a reference
static SingletonClass& instance() {
// static local variable are initialized one time.
static SingletonClass inst;
// We return the instance, which is the same for every calls.
return inst;
}
private:
// Private since we don't want other class to create instances
SingletonClass() = default;
// Our class is not copiable or movable
SingletonClass(const SingletonClass&) = delete;
SingletonClass(SingletonClass&&) = delete;
SingletonClass& operator=(const SingletonClass&) = delete;
SingletonClass& operator=(SingletonClass&&) = delete;
};
You can use your class like that:
auto& myInstance = SingletonClass::instance();
Bonus: It doesn't use dynamic allocation, it's thread safe, and is much simpler.
try this solution:
class Singleton {
private:
static Singleton* instance;
Singleton() {} // must be private
public:
static Singleton* getInstance() {
if (instance == NULL)
instance = new Singleton();
return instance;
}
void method() { cout << "Method of the singleton class\n"; }
};
Singleton* Singleton::instance = NULL;

Error in Singleton pattern implementation

I see errors like
src/singleton.cxx:16:error: invalid use of member 'Singleton::instance' in static member function
src/singleton.cxx:28:error: from this location
src/singleton.cxx:16:error: invalid use of member 'Singleton::instance' in static member function
src/singleton.cxx:29:error: from this location
src/singleton.cxx:16:error: invalid use of member 'Singleton::instance' in static member function
src/singleton.cxx:31:error: from this location
src/singleton.cxx: In function 'int main()':
Now after making changes I get the following errors
singleton-rhel6.3.o: In function Singleton::get_instance()':
src/singleton.cxx:27: undefined reference toSingleton::instance'
#include <cstddef>
class Singleton {
private:
Singleton();
static Singleton * instance;
int m_num;
int incr_call();
public :
static Singleton * get_instance();
};
Singleton * Singleton::get_instance() {
if(instance == NULL)
instance = new Singleton();
return instance;
}
Singleton::Singleton():
m_num(0)
{
}
int Singleton::incr_call() {
return m_num++;
}
int main() {
Singleton * s = Singleton::get_instance();
return 0;
}
instance should be static since you want to be able to call it in get_instance. Also, instance should be private:
class Singleton {
public :
static Singleton * get_instance();
private:
Singleton();
static Singleton * instance;
int m_num;
int incr_call();
};
Singleton* Singleton::instance;
You should change your constructor too, to not initialize instance:
Singleton::Singleton():
m_num(0)
{ }
Because instance is static default initialization is done and it will be NULL / nullptr.
If you have to use singleton, use Meyers' one:
class Singleton {
private:
Singleton() = default;
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
public :
static Singleton& get_instance()
{
static Singleton instance;
return instance;
}
// Extra stuff
};

Undefined reference to Singleton::Singleton() [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I'm trying to get the first basic singleton example from Design Patterns
working, but this has me stumped.
This code compiles cleanly with g++ -c Singleton.cpp:
class Singleton {
public:
static Singleton* Instance();
protected:
Singleton();
private:
static Singleton* _instance;
};
Singleton* Singleton::_instance = 0;
Singleton* Singleton::Instance() {
if (_instance == 0) {
_instance = new Singleton;
}
return _instance;
}
But when I add a skeletal main() and compile with g++ Singleton.cpp I get undefined reference to 'Singleton::Singleton()'.
What am I missing?
You never added a definition for
Singleton();
Which is used in Singleton* Singleton::Instance() by
_instance = new Singleton;
Typically you should and can layout a singleton like:
class Singleton {
public:
static Singleton* Instance() { static Singleton s; return &s; }
Singleton(const Singleton&) = delete;
void operator=(const Singleton&) = delete;
private:
Singleton() = default;
};

Access singleton class methods in blackberry 10

I have implemented Singleton class method hpp and cpp like the following
Singleton.hpp
class Singleton {
private:
Singleton();
public:
virtual ~Singleton();
static Singleton &instance();
int getMemberField();
void setMemberField(int mf);
private:
static Singleton *p_instance;
int m_memberField;
};
Singleton.cpp
Singleton* Singleton::p_instance=NULL ;
Singleton::Singleton() {
p_instance = this;
m_memberField = 0;
}
Singleton::~Singleton() {
p_instance = NULL;
}
Singleton& Singleton::instance() {
if (p_instance==NULL) {
p_instance = new Singleton();
}
return *p_instance;
}
int Singleton::getMemberField(){
return m_memberField;
}
void Singleton::setMemberField(int mf){
m_memberField = mf;
}
My problem is how to access those methods either set or get in application classes.
Please help,
Singleton::instance().setMemberField(42);