2010-03-31 5 views
2

Je tente de créer une classe de motif singleton multi-thread.Boost C++ Singleton erreur LNK2001: symbole externe non résolu "privé: statique long Nsp :: HL :: drapeau" (? Flag @ HL @ Nsp @@ 0JA)

tête:

class HL{ 

    public: 
     static HL* getInstance(); 
    ......... 
    private: 
     static HL* instance; 
     static boost::once_flag flag; 
     HL(); 
     static void initOnce(); 
} 

CPP:

HL* HL::instance = NULL; 

HL* HL::getInstance(){ 
    if(instance == NULL){ 
     boost::call_once(flag, initOnce); 
    } 
    return instance; 
} 

void HL::initOnce(){ 
    instance = new HL(); 
} 

Je reçois cette erreur:

 
error LNK2001: unresolved external symbol "private: static long Nsp::HL::flag" ([email protected]@[email protected]@0JA) 

Qu'est-ce qui ne va pas?

Répondre

7

Vous devez définir la variable membre statique dans le fichier cpp:

boost::once_flag Nsp::HL::flag; 

Vous pouvez l'initialiser si vous avez besoin (je ne l'ai pas utilisé boost::once_flag, et ne peut pas vous dire si comment il doit être initialisé):

boost::once_flag Nsp::HL::flag = {whatever goes here}; 
+0

Merci! Ça marche. 'boost :: once_flag HL :: flag = BOOST_ONCE_INIT;' –