IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

probleme avec singleton


Sujet :

C++

  1. #1
    Membre confirmé Avatar de donkeyquote
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    195
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 195
    Par défaut probleme avec singleton
    Bonjour à tous !!

    Je suis en train d'implementer un singleton de la façon suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    29
    30
    31
    32
    33
    34
    35
    36
    37
     
    #ifndef GLOBAL_STAT_YEAR_FILE_H
    #define GLOBAL_STAT_YEAR_FILE_H
     
    #include <iostream>
     
    class Player;
     
    // GlobalStat implementation as a singleton
     
    Header :
     
    class GlobalStat
    {
     
    public:
    	~GlobalStat();
     
    // instanciates a GlobalStat
    	static void init(); 
     
    // retrieve the static created object  
    	static GlobalStat * getInstance();
     
    // write the stat on the filestatic GlobalStat * _instance;
    	void makeStat(Player * iPlayer, int iYear, const std::string & iNameFile="stat_year.txt");
     
    private:
    	GlobalStat();
            GlobalStat(const GlobalStat&);
          	GlobalStat& operator= (const GlobalStat&);
     
    	static GlobalStat * _instance;
     
    };
     
    #endif // GLOBAL_STAT_YEAR_FILE_H
    Et pour l'mplementation :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    29
    30
     
    #include "GlobalStat.h"
    #include "Player.h"
     
    // pointer variable containing a GlobalStat intance
    GlobalStat * _instance = NULL;
     
    GlobalStat::GlobalStat()
    {
    }
     
    GlobalStat::~GlobalStat()
    {
    }
     
    void GlobalStat::init()
    {
    	if (_instance == NULL)
    		_instance = new GlobalStat;		
    }
     
    GlobalStat * GlobalStat::getInstance()
    {
    	return _instance;
    }
     
    void GlobalStat::makeStat(Player * iPlayer, int iYear, const std::string & iNameFile)
    {
    	std::cout << "This is the creation of a file" << std::endl;
    }
    Et je trouve les erreurs :

    GlobalStat.o: In function `GlobalStat::getInstance()':
    GlobalStat.cpp.text+0x1c): undefined reference to `GlobalStat::_instance'
    GlobalStat.o: In function `GlobalStat::init()':
    GlobalStat.cpp.text+0xc8): undefined reference to `GlobalStat::_instance'
    GlobalStat.cpp.text+0xe8): undefined reference to `GlobalStat::_instance'
    collect2: ld returned 1 exit status

    Pourquoi ? Comment je peux l'éviter ?

    Merci !

  2. #2
    Rédacteur

    Avatar de Davidbrcz
    Homme Profil pro
    Ing Supaéro - Doctorant ONERA
    Inscrit en
    Juin 2006
    Messages
    2 307
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : Suisse

    Informations professionnelles :
    Activité : Ing Supaéro - Doctorant ONERA

    Informations forums :
    Inscription : Juin 2006
    Messages : 2 307
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    GlobalStat* GlobalStat::_instance = NULL;
    "Never use brute force in fighting an exponential." (Andrei Alexandrescu)

    Mes articles dont Conseils divers sur le C++
    Une très bonne doc sur le C++ (en) Why linux is better (fr)

Discussions similaires

  1. PROBLEME AVEC SINGLETON
    Par Munoz33 dans le forum VC++ .NET
    Réponses: 1
    Dernier message: 26/03/2008, 08h28
  2. [LINKAGE] probleme avec Singleton
    Par magnus2005 dans le forum C++
    Réponses: 10
    Dernier message: 03/04/2007, 16h34
  3. probleme avec la touche F10
    Par b.grellee dans le forum Langage
    Réponses: 2
    Dernier message: 15/09/2002, 22h04
  4. Probleme avec fseek
    Par Bjorn dans le forum C
    Réponses: 5
    Dernier message: 04/08/2002, 07h17
  5. [Kylix] probleme avec un imagelist
    Par NicoLinux dans le forum EDI
    Réponses: 4
    Dernier message: 08/06/2002, 23h06

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo