Bonjour,
J'ai une erreur que je ne comprends pas dans ma (non-)compilation : j'essaie de templatiser mon object factory. Dans mon .h j'ai ceci :
et dans le main :
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 #ifndef _FACTORY_H_ #define _FACTORY_H_ #include <map> #include <iostream> #include <cstdlib> #include <string> //#include "AbstractConstitutiveLaws.h" template<typename AClass> class Factory { public : typedef AClass* (*CreateFunc)(); bool Register(const std::string& id, CreateFunc creator ) { return associations_.insert(AssocMap::value_type(id, creator)).second; } bool Unregister(const std::string& id) { return associations_.erase(id) == 1; } AClass* CreateObject(const std::string& id) { AssocMap::const_iterator i = associations_.find(id); if(i != associations_.end()){ return (i->second)(); } else{ std::cout << "Unknown Type in object factory" << std::endl; exit(1); } } static Factory & Instance() { static Factory obj; //! \bug what happens in multithreaded env ? return obj; } private: typedef std::map<std::string,CreateFunc> AssocMap; std::map<std::string,CreateFunc> associations_; //Singleton implies creation/destruction entirely private Factory(){} Factory(const Factory&); Factory& operator=(const Factory&); ~Factory(){} }; #endif
Le tout me produit les erreurs suivantes :
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 #include "ObjectFactory.h" #include "AbstractConstitutiveLaws.h" using namespace std; int main(void) { //Factory<AbstractConstitutiveLaws,string,> cout << "coucou" << endl; AbstractConstitutiveLaws * pBase = 0; string temp("cl1"); pBase = Factory<AbstractConstitutiveLaws>::Instance().CreateObject(temp); cout << "Créés par la factory" << endl; pBase->Compute(); temp = "cl2"; delete pBase; pBase = Factory<AbstractConstitutiveLaws>::Instance().CreateObject(temp); pBase->Compute(); delete pBase; return 1; }
Est-ce que qqn aurait une idée sur pourquoi mon itérateur i n'est pas instancié(able) dans la partie object factory ? Je suis un peu largué, je dois dire...In file included from es.cpp:2:
ObjectFactory.h: In member function ‘AClass* Factory<AClass>::CreateObject(const std::string&)’:
ObjectFactory.h:28: erreur: expected `;' before ‘i’
ObjectFactory.h:30: erreur: ‘i’ was not declared in this scope
ObjectFactory.h: In member function ‘AClass* Factory<AClass>::CreateObject(const std::string&) [with AClass = AbstractConstitutiveLaws]’:
es.cpp:11: instantiated from here
ObjectFactory.h:28: erreur: dependent-name ‘std::map::const_iterator’ is parsed as a non-type, but instantiation yields a type
ObjectFactory.h:28: note: say ‘typename std::map::const_iterator’ if a type is meant
ObjectFactory.h: At global scope:
ObjectFactory.h: In instantiation of ‘AClass* Factory<AClass>::CreateObject(const std::string&) [with AClass = AbstractConstitutiveLaws]’:
es.cpp:11: instantiated from here
ObjectFactory.h:26: attention : unused parameter ‘id’
make: *** [es.o] Erreur 1
Merci
Hugo
Partager