Pages : 1
#1 Le 08/08/2007, à 19:33
- Sebome
Cpp template et map
Bonjour à tous.
Je me suis lancé a faire un petit programme pour apprendre a me servir des map.
J'ai essayé aussi de faire des fonctions template pour afficher et supprimer la map mais j'ai des erreurs de compilation que je ne comprend pas (surtout que j'ai déjà fait cela une fois sur une autre machine et ca a marché). Voici le code :
#include <iostream>
#include <map>
#include <string>
using namespace std;
typedef map<int, string> MaMap;
void addMap( MaMap*, int ,string );
template <class X, class T>
void printMap( map<X, T*>* & );
template <class X, class T>
void clean( map<X, T*>* & );
int main( int argc, char** argv )
{
if( argc <= 1 )
{
cout << "Arguments?" << endl;
return 1;
}
MaMap* myMap = new MaMap;
for( int i = 0; i < argc; i++ )
{
addMap( myMap, i, argv[i] );
}
printMap( myMap );
return 0;
}
void addMap(MaMap* m, int i, string s )
{
pair<MaMap::iterator, bool>res = m->insert( MaMap::value_type( i, s ));
if( res.second == false )
{
cout << "Probleme ajout map" << endl;
}
}
template <class X, class T>
void printMap( map<X,T>* &m )
{
if( m == NULL )
return;
for( map<X,T>::iterator i = m->begin() ; i != m->end() ; i++ )
{
cout << (*i).first << "\t" << (*i).second << endl;
}
}
template <class X, class T>
void clean(map<X, T>* &theMap)
{
if( theMap == NULL )
return;
for(map<X, T>::iterator i = theMap->begin() ; i != theMap->end() ; i++ )
{
if( (*i).second == NULL )
continue;
delete (*i).second;
(*i).second = NULL;
}
theMap->clear();
delete theMap;
theMap = NULL;
}
et voici les erreurs de compilation :
main.cpp: In function «int main(int, char**)":
main.cpp:28: erreur: no matching function for call to «printMap(MaMap*&)"
main.cpp: In function «void printMap(std::map<X, T, std::less<_Key>, std::allocator<std::pair<const _Key, _Tp> > >*&)":
main.cpp:48: erreur: expected `;' before «i"
main.cpp:48: erreur: «i" was not declared in this scope
main.cpp: In function «void clean(std::map<X, T, std::less<_Key>, std::allocator<std::pair<const _Key, _Tp> > >*&)":
main.cpp:59: erreur: expected `;' before «i"
main.cpp:59: erreur: «i" was not declared in this scope
make: *** [main.o] Erreur 1
Quelqu'un à une idée sur mon problème?
Je vous remercie d'avance
Cordialement
Sebome
Hors ligne
Pages : 1