单身人士不工作

时间:2014-03-11 09:43:20

标签: c++ singleton

我已经用书写了这个Singleton演示程序,但它似乎没有编译。我从编译器收到关于包含单个Singleton实例的单例类中的静态变量的链接器错误。可能是什么原因?

#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

}

错误:

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 个答案:

答案 0 :(得分:5)

您必须在m_singleton文件中定义静态Singleton.cpp字段:

Singleton::m_instance = NULL;

但我会像这样重写你的Instance()方法:

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

并完全删除m_instance字段。这样做的好处是,当程序关闭时,您的单例实例将被正确删除。就像现在一样,你的单例实例永远不会被删除,更重要的是,它的析构函数永远不会被调用。因此,如果您决定向Singleton类添加(非默认)析构函数,例如清理某些资源,则不会调用它。

我还会返回对单例而不是指针的引用,因为它比返回指针更好地传递所有权语义(这可能会提示客户端删除单例实例!)。

相关问题