Cpp template et map

sebome Messages postés 15 Date d'inscription mardi 10 mai 2005 Statut Membre Dernière intervention 5 septembre 2008 - 8 août 2007 à 20:49
sebome Messages postés 15 Date d'inscription mardi 10 mai 2005 Statut Membre Dernière intervention 5 septembre 2008 - 8 août 2007 à 23:16
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 .
Je suis sur linux.
Voici le code :

#include
#include <map>
#include <string>
using namespace std;

typedef map 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 );
  clean( 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 la compil :
main.cpp: In function «int main(int, char**)":
main.cpp:28: erreur: no matching function for call to «printMap(MaMap*&)"
main.cpp:29: erreur: no matching function for call to «clean(MaMap*&)"
main.cpp: In function «void printMap(std::map<X, T, std::less<_Key>, std::allocator<std::pair<const _Key, _Tp> > >*&)":
main.cpp:49: erreur: expected `;' before «i"
main.cpp:49: 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:60: erreur: expected `;' before «i"
main.cpp:60: 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

Sébome

4 réponses

vecchio56 Messages postés 6535 Date d'inscription lundi 16 décembre 2002 Statut Membre Dernière intervention 22 août 2010 14
8 août 2007 à 21:58
Il faut définir tes fonctions avant main (une simple déclaration ne suffit pas).
Par ailleurs, (*i).second est de type string, tu ne peux donc pas le comparer à NULL, ni lui appliquer l'opérateur delete

_____________________________________
Un éditeur de ressources gratuit pour Windows
0
sebome Messages postés 15 Date d'inscription mardi 10 mai 2005 Statut Membre Dernière intervention 5 septembre 2008
8 août 2007 à 22:34
J'ai mis mais fonction avant le main.
Oui je suis con pour la comparaison du (*i).second avec NULL, c'est parce que je voyé déja l'utiliser avec des pointeur sur objet...
Par contre j'ai toujours des problèmes de compilation...
#include
#include <map>
#include <string>
using namespace std;

typedef map MaMap;

void addMap( MaMap*, int ,string );

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;
    }
}

int main( int argc, char** argv )
{

  if( argc <= 1 )
    {
      cout << "Arguments?" << endl;
      return 1;
    }
  MaMap* myMap = new MaMap;
  if( myMap == NULL )
    return 1;
  for( int i = 0; i < argc; i++ )
    {
      addMap( myMap, i, argv[i] );
    }
  printMap( myMap );
  delete 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;
    }
}

main.cpp: In function «void printMap(std::map<X, T, std::less<_Key>, std::allocator<std::pair<const _Key, _Tp> > >*&)":
main.cpp:15: erreur: expected `;' before «i"
main.cpp:15: erreur: «i" was not declared in this scope
main.cpp: In function «void printMap(std::map<X, T, std::less<_Key>, std::allocator<std::pair<const _Key, _Tp> > >*&) [with X = int, T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]":
main.cpp:36:   instantiated from here
main.cpp:15: erreur: dependent-name «std::map<X,T,std::less<_Key>,std::allocator<std::pair<const _Key, _Tp> > >::iterator" is parsed as a non-type, but instantiation yields a type
main.cpp:15: note: say «typename std::map<X,T,std::less<_Key>,std::allocator<std::pair<const _Key, _Tp> > >::iterator" if a type is meant
make: *** [main.o] Erreur 1

Sébome
0
vecchio56 Messages postés 6535 Date d'inscription lundi 16 décembre 2002 Statut Membre Dernière intervention 22 août 2010 14
8 août 2007 à 23:07
Avec Visual C++ 2005, je n'ai pas d'erreur avec le code que tu as mis ci-dessus
Tu peux essayer de sortir la déclaration de i du for...

_____________________________________
Un éditeur de ressources gratuit pour Windows
0
sebome Messages postés 15 Date d'inscription mardi 10 mai 2005 Statut Membre Dernière intervention 5 septembre 2008
8 août 2007 à 23:16
J'ai déjà essayé de sortir le i du for ca e chane rien.
Moi je suis sur linux je compil avec g++.
J'avais déjà fait un code comme ca sur linux une fois mais c'était avec une version du compilateur plus ancienne et cela fonctionné...
il y a peut être un problème avec mon makefile je le mets au cas ou...
CC=g++
CFLAGS=-W -Wall
LDFLAGS=
EXEC=test.x
SRC= $(wildcard *.cpp)
OBJ= $(SRC:.cpp=.o)

all: $(EXEC)

test.x: $(OBJ)
        @$(CC) -o $@ $^ $(LDFLAGS)

%.o: %.cpp
        @$(CC) -o $@ -c $< $(CFLAGS)

clean:
        @rm -rf *.o
        @rm -rf *~

rmfull: clean
        @rm -rf $(EXEC)

En tout cas merci beaucoup pour ton aide!

Sébome
un peu de programmation en C et en VB et de photoshop
0
Rejoignez-nous