Problèmes écriture de classes templates - Compatibilité VS2008 / g++
Bonjour,
Encore une fois, j'ai de gros problèmes lors de l'écriture de ma classe template.
Voici ma classe:
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 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
#include <list>
#include <map>
#include <string>
#include <typeinfo>
#include <cassert>
#include "Game/UnitTemplate.h"
#include "Game/UnitTemplateFactionList.h"
#include "Utils/Logger.h"
#include "Utils/Exceptions/LibraryException.h"
template <typename T>
class Library
{
private:
std::map < std::string, T* > entries; /*!< */
public:
~Library()
{
for ( std::map< std::string, T* >::iterator itPair = entries.begin() ; itPair != entries.end() ; ++itPair )
{
delete (itPair->second);
}
LDebug << "Library of " << typeid(T).name() << " free";
}
void add(const std::string& name, T* const value)
{
if ( this->exists(name) )
{
LWarning << "Library will overwrite the key '" << name << "'";
}
entries[name] = value;
}
const T* get(const std::string& name)const
{
std::map<std::string, T*>::const_iterator itT = entries.find(name);
if (itT == entries.end())
{
throw LibraryException("Element '" + name + "' not found");
}
return itT->second;
}
bool exists(const std::string& name)const
{
if ( entries.count(name) != 0 )
{
return true;
}
return false;
}
// template <typename T>
void getValues(std::list< const T* >* pListItems)const
{
for (std::map< std::string, T* >::const_iterator itItem = entries.begin() ;
itItem != entries.end() ; ++itItem )
{
pListItems->push_back(itItem->second);
}
}
};
//template <>
//class Library<UnitTemplate>
//{
//private:
//std::map < std::string, UnitTemplateFactionList* > entries; /*!< */
//public:
//~Library()
//{
//for ( std::map< std::string, UnitTemplateFactionList* >::iterator itPair = entries.begin() ; itPair != entries.end() ; ++itPair )
//{
//delete (itPair->second);
//}
//LDebug << "Library of " << typeid(UnitTemplateFactionList).name() << " free";
//}
//void add(const std::string& name, UnitTemplate* const value)
//{
//if ( !this->exists(name) )
//{
//entries[name] = new UnitTemplateFactionList();
//if ( entries[name] == NULL )
//{
//LError << "Fail to allocate memory for UnitTemplateFactionList";
//throw std::bad_alloc("UnitTemplateFactionList allocation failed");
//}
//}
//entries[name]->add(value);
//}
//const UnitTemplateFactionList* get(const std::string& name)const
//{
//std::map<std::string, UnitTemplateFactionList*>::const_iterator itT = entries.find(name);
//if (itT == entries.end())
//{
//return NULL;
//}// ToDO throw exception ?
//return itT->second;
//}
//bool exists(const std::string& name)const
//{
//if ( entries.find(name) != entries.end() )
//{
//return true;
//}
//return false;
//}
//template <typename T>
//void getValues(std::list< const UnitTemplateFactionList* >* pListItems)const
//{
//for (std::map< std::string, UnitTemplateFactionList* >::const_iterator itItem = entries.begin() ;
//itItem != entries.end() ; ++itItem )
//{
//pListItems->push_back(itItem->second);
//}
//}
//}; |
Le compilateur (g++ 4.4) me donne les erreurs suivantes:
Citation:
In file included from Engine/Library.cpp:25:
Engine/Library.h: In destructor ‘Library<T>::~Library()’:
Engine/Library.h:51: error: expected ‘;’ before ‘itPair’
Engine/Library.h:51: error: ‘itPair’ was not declared in this scope
Engine/Library.h: In member function ‘const T* Library<T>::get(const std::string&) const’:
Engine/Library.h:70: error: expected ‘;’ before ‘itT’
Engine/Library.h:72: error: ‘itT’ was not declared in this scope
Engine/Library.h:77: error: ‘itT’ was not declared in this scope
Engine/Library.h: In member function ‘void Library<T>::getValues(std::list<const T*, std::allocator<const T*> >*) const’:
Engine/Library.h:93: error: expected ‘;’ before ‘itItem’
Engine/Library.h:94: error: ‘itItem’ was not declared in this scope
Mon plus gros problème, c'est que je crois que ce code compile sous VS 2008.
Je souhaite vraiment avoir un code compatible avec les deux compilateurs.
Ce qui me chagrine, c'est que le compilateur indique comme quoi T n'est pas défini, pourtant le template <typename T>.