when i try to link a SIngleton class a get
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
the Singleton class:
#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;
}
}
i need some LDFLAGS to the linker?
You can only declare methods static in the declarations, it's not allowed in the implementations. Just change your implementations to;
Singleton& Singleton::Instance()
{
and
void Singleton::Destroy()
{
and you should be fine.
You need to remove static from the definitions in the cpp file: the compiler already knows the Singleton::Instance and Singleton::Destroy are static from their declarations. Everything else looks right.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With