sebome
Messages postés15Date d'inscriptionmardi 10 mai 2005StatutMembreDernière intervention 5 septembre 2008
-
8 août 2007 à 20:49
sebome
Messages postés15Date d'inscriptionmardi 10 mai 2005StatutMembreDerniè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;
}
}
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
vecchio56
Messages postés6535Date d'inscriptionlundi 16 décembre 2002StatutMembreDernière intervention22 août 201014 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
sebome
Messages postés15Date d'inscriptionmardi 10 mai 2005StatutMembreDerniè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;
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
sebome
Messages postés15Date d'inscriptionmardi 10 mai 2005StatutMembreDerniè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