Implémentation du design pattern singleton
Voilà le problème est dans le titre : le compilateur n'est pas content avec :
Singleton.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #ifndef SINGLETON_H
#define SINGLETON_H
#include <iostream>
using namespace std;
template <typename T> class Singleton
{
private:
// Unique instance
static T* _singleton;
protected:
// Constructeur/destructeur
Singleton ();
~Singleton ();
public:
// Interface publique
static T* getInstance ();
static void kill ();
};
#endif |
-------------------------------------------------
Et Singleton.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include "Singleton.h"
template <typename T> T* Singleton<T>::_singleton = NULL;
// Constructeur/destructeur
Singleton::Singleton<T> () { }
Singleton::~Singleton () { }
// Interface publique
static T* Singleton::getInstance ()
{
if (_singleton == NULL)
_singleton = new T();
return (static_cast<T*> (_singleton));
}
static void Singleton::kill ()
{
if (NULL != _singleton)
{
delete (_singleton);
_singleton = NULL;
}
} |
-----------------------------------------------------
Je ne comprend pas les erreurs de compilation, help me please !
src/Singleton.cpp:7: erreur: «template<class T> class Singleton" used without template parameters
src/Singleton.cpp:7: erreur: expected constructor, destructor, or type conversion before «<" token
src/Singleton.cpp:8: erreur: expected constructor, destructor, or type conversion before «::" token
src/Singleton.cpp:11: erreur: expected initializer before «*" token
src/Singleton.cpp:19: erreur: «template<class T> class Singleton" used without template parameters
src/Singleton.cpp: In function «void kill()":
src/Singleton.cpp:21: erreur: «_singleton" was not declared in this scope
src/Singleton.cpp:23: erreur: type «<type error>" argument given to «delete", expected pointer
balises [code] ajoutées par r0d. Merci d'y penser la prochaine fois.