-1

I have written this Singleton demonstration program 'by the book', but it does not seem to be compiling. I am getting a linker error from the compiler concerning the static variable inside the singleton class that holds the single Singleton instance. What might be the reason?

#include <iostream>
#include "Singleton.h"

//using namespace std;

int main()
{

    Singleton * sinObj1 = Singleton::Instance();
    sinObj1 -> setInt (3);
    Singleton * sinObj2 = Singleton::Instance();

    std::cout << sinObj2 -> getInt() << std::endl;

    return 0;
}

/*
 * Singleton.h
 *
 *  Created on: Feb 26, 2014
 *      Author: edwinrietmeijer
 */

#ifndef SINGLETON_H_
#define SINGLETON_H_

#include <iostream>

class Singleton {
public:
    static Singleton* Instance();
    void setInt( int );
    int getInt();
    void method();
private:
    Singleton();
    Singleton( const Singleton& );
    Singleton& operator=( Singleton const& );
    static Singleton* m_instance;
    int anInt;
};

#endif /* SINGLETON_H_ */

/*
 * Singleton.cpp
 *
 *  Created on: Feb 26, 2014
 *      Author: edwinrietmeijer
 */

#include "Singleton.h"

Singleton* Singleton::Instance()
{
    if ( !m_instance )   // Only allow one instance of class to be generated.
        m_instance = new Singleton;

    return m_instance;
}

void Singleton::method() {
    std::cout << "Method" << std::endl;
}

void Singleton::setInt( int i ) {
    anInt = i;
}

int Singleton::getInt() {
    return anInt;
}

Singleton::Singleton() : anInt( 0 ){
    // TODO Auto-generated constructor stub

}

Error:

Building target: SingletonTest

Invoking: MacOS X C++ Linker

g++  -o "SingletonTest"  ./src/Singleton.o ./src/SingletonTest.o   

Undefined symbols for architecture x86_64:

  "Singleton::m_instance", referenced from:

      Singleton::Instance() in Singleton.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

make: *** [SingletonTest] Error 1

1 Answer 1

5

You have to define the static m_singleton field like so in your Singleton.cpp file:

Singleton::m_instance = NULL;

But I would rewrite your Instance() method like so:

Singleton* Singleton::Instance()
{
    static Singleton instance;
    return &instance;
}

and remove the m_instance field altogether. This has the benefit that your singleton instance will be properly deleted when the program shuts down. As it is right now, your singleton instance will never be deleted, and more importantly, its destructor will never be called. So if you decide to add a (non-default) destructor to your Singleton class, for example to clean up some resources, it will not be called.

I would also return a reference to the singleton instead of a pointer, as it communicates the ownership semantics better than returning a pointer (which might prompt a client to delete the singleton instance!).

1
  • Singleton * Singleton::m_instance = NULL; compiles but I will use the reference method. Thanks for the tips!
    – staxas
    Mar 11, 2014 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.