当我尝试链接一个 Singleton 类时,我得到了以下信息:
我需要为链接器添加一些LDFLAGS吗?
sources/singleton.cpp:8:41: error: no se puede declarar que la función miembro ‘static myspace::Singleton& myspace::Singleton::Instance()’ tenga enlace estático [-fpermissive]
sources/director.cpp:19:35: error: no se puede declarar que la función miembro ‘static void myspace::Singleton::Destroy()’ tenga enlace estático [-fpermissive]
myspace: *** [objects/singleton.o] Error 1
单例模式类:
#Singleton.h
#include <stdlib.h>
namespace myspace {
class Singleton
{
public:
static Singleton& Instance();
virtual ~Singleton(){};
private:
static Singleton* instance;
static void Destroy();
Singleton(){};
Singleton(const Singleton& d){}
};
}
#Singleton.cpp
#include "../includes/Singleton.h"
namespace myspace {
Singleton* Singleton::instance = 0;
static Singleton& Singleton::Instance()
{
if(0 == instance) {
instance = new Singleton();
atexit(&Destroy);
}
return *instance;
}
static void Singleton::Destroy()
{
if (instance != 0) delete instance;
}
}
我需要为链接器添加一些LDFLAGS吗?
LANG=C make
将为您提供一个可搜索和可发布的良好错误字符串。 - Rawler