Code: Alles auswählen
#ifndef SINLGETON
#define SINLGETON
#include<assert.h>
#include "core_global.h"
template<typename T>
class CORE_EXPORT Singleton
{
static T* ms_singleton;
public:
Singleton()
{
assert(!ms_singleton);
int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
ms_singleton = (T*)((int)this + offset);
}
~Singleton()
{
assert(ms_singleton);
ms_singleton=0;
}
static T& get()
{
assert(ms_singleton);
return *ms_singleton;
}
static T* getPtr()
{
assert(ms_singleton);
return ms_singleton;
}
};
template <typename T> T* Singleton <T>::ms_singleton = 0;
#endif
Code: Alles auswählen
#ifndef CORE_GLOBAL_H
#define CORE_GLOBAL_H
#include <Qt/qglobal.h>
#ifdef CORE_LIB
# define CORE_EXPORT Q_DECL_EXPORT
#else
# define CORE_EXPORT Q_DECL_IMPORT
#endif
#endif // CORE_GLOBAL_H
Wenn ich sie aber in einem anderen Projekt verwenden will bekomme ich folgenden fehler:
singleton.h(39) : error C2491: 'Singleton<T>::ms_singleton': Definition von Statisches Datenmember für dllimport nicht zulässig
in der msdn steht folgendes:
http://msdn.microsoft.com/en-us/library ... S.71).aspx
Data, static data members, and functions can be declared as dllimports but not defined as dllimports.
Was muss ich denn jetzt an der definiton der statischen variable ms_sinlgeton ändern damit das mit dem import richtig klappt.