IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

vector et push_back


Sujet :

C++

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2009
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2009
    Messages : 49
    Points : 38
    Points
    38
    Par défaut vector et push_back
    Rebonjour

    Suit à mon sujet précédent, j'ai toujours une satanée erreur du à une insertion d'un objet dans un vecteur. Je vous poste mon code et je vous explique après.

    Contrainte.cpp

    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
     
    #include <iostream>
    #include "Contrainte.h"
    #include "Recherche.h"
     
    using namespace std;
     
    Contrainte::Contrainte ( Recherche* recherche , int niv )
    {
    	objet = recherche ;
    	niveau = niv ;
    }
     
    bool Contrainte::isValide ()
    {
    	return ( (*objet).getNiveau() >= niveau );
    }
    Contrainte.h

    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
     
    #ifndef CONTRAINTE_H
    #define CONTRAINTE_H
     
    class Recherche;
     
    class Contrainte
    {
    	private:
     
    	Recherche* objet ;
    	int niveau ;
     
    	public:
     
    	Contrainte ( Recherche* recherche , int niv );
    	bool isValide();
    };
     
    #endif
    Recherche.cpp

    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
     
    #include <iostream>
    #include <vector>
    #include <string>
    #include "Contrainte.h"
    #include "Recherche.h"
     
    using namespace std;
     
    Recherche::Recherche( std::string NOM , int GID , int METAL , int CRISTAL , int DEUT )
    {
    	nom = NOM ;
    	gid = GID ;
    	metal = METAL ;
    	cristal = CRISTAL ;
    	deuterium = DEUT ;
    	niveau = 0 ;
    }
     
    int Recherche::getNiveau()
    {
    	return niveau ;
    }
     
    void Recherche::setNiveau( int niv )
    {
    	niveau = niv ;
    }
     
    void Recherche::addContrainte ( Recherche* objet , int niv )
    {
    	contraintes.push_back( Contrainte( objet , niv ) );
    }
    Recherche.h

    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
     
    #ifndef RECHERCHE_H
    #define RECHERCHE_H
     
    #include <vector>
    #include <string>
    #include "Contrainte.h"
     
     
    class Recherche
    {
    	private:
     
    	std::string nom ;
    	int gid ;
    	int metal ;
    	int cristal ;
    	int deuterium ;
    	int niveau ;
    	std::vector<Contrainte> contraintes ;
     
    	public:
     
    	Recherche( std::string NOM , int GID , int METAL , int CRISTAL , int DEUT );
    	int getNiveau ();
    	void setNiveau ( int niv );
    	void addContrainte ( Recherche* objet , int niv );
    };
     
    #endif
    Donc chaque Recherche possède des contraintes avant sa construction ( dont on ne connait pas le nombre) et chaque contrainte pointe sur un batiment existant.

    Le problème provient (d'après mon interprétation du message d'erreur) de l'ajout d'une contrainte. Je vous donne mon message d'erreur

    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
     
    /usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o: In function `_start':
    /build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf/start.S:115: undefined reference to `main'
    /tmp/ccz9mBVk.o: In function `__static_initialization_and_destruction_0(int, int)':
    Contrainte.cpp:(.text+0x69): undefined reference to `std::ios_base::Init::Init()'
    Contrainte.cpp:(.text+0x6e): undefined reference to `std::ios_base::Init::~Init()'
    /tmp/ccz9mBVk.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
    /tmp/ccw6kYOE.o: In function `Recherche::Recherche(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int, int, int)':
    Recherche.cpp:(.text+0xf): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
    Recherche.cpp:(.text+0x2f): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
    Recherche.cpp:(.text+0x8b): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
    /tmp/ccw6kYOE.o: In function `Recherche::Recherche(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int, int, int)':
    Recherche.cpp:(.text+0xab): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
    Recherche.cpp:(.text+0xcb): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
    Recherche.cpp:(.text+0x127): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
    /tmp/ccw6kYOE.o: In function `__static_initialization_and_destruction_0(int, int)':
    Recherche.cpp:(.text+0x1a5): undefined reference to `std::ios_base::Init::Init()'
    Recherche.cpp:(.text+0x1aa): undefined reference to `std::ios_base::Init::~Init()'
    /tmp/ccw6kYOE.o: In function `std::vector<Contrainte, std::allocator<Contrainte> >::_M_insert_aux(__gnu_cxx::__normal_iterator<Contrainte*, std::vector<Contrainte, std::allocator<Contrainte> > >, Contrainte const&)':
    Recherche.cpp:(.text._ZNSt6vectorI10ContrainteSaIS0_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS0_S2_EERKS0_[std::vector<Contrainte, std::allocator<Contrainte> >::_M_insert_aux(__gnu_cxx::__normal_iterator<Contrainte*, std::vector<Contrainte, std::allocator<Contrainte> > >, Contrainte const&)]+0x227): undefined reference to `__cxa_begin_catch'
    Recherche.cpp:(.text._ZNSt6vectorI10ContrainteSaIS0_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS0_S2_EERKS0_[std::vector<Contrainte, std::allocator<Contrainte> >::_M_insert_aux(__gnu_cxx::__normal_iterator<Contrainte*, std::vector<Contrainte, std::allocator<Contrainte> > >, Contrainte const&)]+0x288): undefined reference to `__cxa_rethrow'
    Recherche.cpp:(.text._ZNSt6vectorI10ContrainteSaIS0_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS0_S2_EERKS0_[std::vector<Contrainte, std::allocator<Contrainte> >::_M_insert_aux(__gnu_cxx::__normal_iterator<Contrainte*, std::vector<Contrainte, std::allocator<Contrainte> > >, Contrainte const&)]+0x291): undefined reference to `__cxa_end_catch'
    /tmp/ccw6kYOE.o: In function `std::vector<Contrainte, std::allocator<Contrainte> >::_M_check_len(unsigned int, char const*) const':
    Recherche.cpp:(.text._ZNKSt6vectorI10ContrainteSaIS0_EE12_M_check_lenEjPKc[std::vector<Contrainte, std::allocator<Contrainte> >::_M_check_len(unsigned int, char const*) const]+0x36): undefined reference to `std::__throw_length_error(char const*)'
    /tmp/ccw6kYOE.o: In function `__gnu_cxx::new_allocator<Contrainte>::deallocate(Contrainte*, unsigned int)':
    Recherche.cpp:(.text._ZN9__gnu_cxx13new_allocatorI10ContrainteE10deallocateEPS1_j[__gnu_cxx::new_allocator<Contrainte>::deallocate(Contrainte*, unsigned int)]+0xd): undefined reference to `operator delete(void*)'
    /tmp/ccw6kYOE.o: In function `__gnu_cxx::new_allocator<Contrainte>::allocate(unsigned int, void const*)':
    Recherche.cpp:(.text._ZN9__gnu_cxx13new_allocatorI10ContrainteE8allocateEjPKv[__gnu_cxx::new_allocator<Contrainte>::allocate(unsigned int, void const*)]+0x24): undefined reference to `std::__throw_bad_alloc()'
    Recherche.cpp:(.text._ZN9__gnu_cxx13new_allocatorI10ContrainteE8allocateEjPKv[__gnu_cxx::new_allocator<Contrainte>::allocate(unsigned int, void const*)]+0x32): undefined reference to `operator new(unsigned int)'
    /tmp/ccw6kYOE.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
    /tmp/ccw6kYOE.o:(.eh_frame+0x4b): undefined reference to `__gxx_personality_v0'
    collect2: ld returned 1 exit status

    PS : je compile en faisant gcc *.cpp

  2. #2
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    Le C++ se compile (et surtout se lie) avec g++, pas gcc.

  3. #3
    Membre actif
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    188
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 188
    Points : 248
    Points
    248
    Par défaut
    il s'agit visiblement d'une erreur de linkage.
    Pourquoi mets-tu des include dans tes cpp ?
    supprime tous les include de Contrainte.cpp sauf

    #include "Contrainte.h"
    #include "Recherche.h"

    et ceux de Recherche.cpp sauf

    #include "Recherche.h"

    --------------EDIT------------
    arf je suis arrivé trop tard ^^
    j'avais même pas vu le gcc

  4. #4
    Membre actif
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 318
    Points : 291
    Points
    291
    Par défaut
    Il manque quelque chose dans ta class Contrainte pour quelle soit utilisable dans un std::vector, Il me semble. Mais je ne suis pas sur.

  5. #5
    Expert éminent sénior
    Avatar de koala01
    Homme Profil pro
    aucun
    Inscrit en
    Octobre 2004
    Messages
    11 614
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : Belgique

    Informations professionnelles :
    Activité : aucun

    Informations forums :
    Inscription : Octobre 2004
    Messages : 11 614
    Points : 30 626
    Points
    30 626
    Par défaut
    Salut.

    Ce qui se passe, c'est que lorsque tu compile avec l'instruction gcc, il va transmettre à l'éditeur de liens (ld) les options d'édition de liens d'applications... C uniquement...

    Cela implique que ld ne cherchera pas parmi les symboles inconnus dans les bibliothèques C++ (classiquement libstdc++.a et libsupc++.a), et comme tout ce qui a trait à C++ se trouve dans ces deux bibliothèques, cela ne peut pas fonctionner

    Tu as donc deux solutions pour résoudre ton problème:
    • continuer à utiliser la commande gcc, mais en ajoutant les drapeaux d'édition de liens qui vont bien ( typiquement -lstdc++ et -lsupc++)
    • utiliser la commande g++, qui compile par défaut avec les options correctes pour le support du langage C++

    N'oublie pas de rajouter les fichiers objets correspondant à tes différentes unités de compilation lorsque tu compile main.cpp
    A méditer: La solution la plus simple est toujours la moins compliquée
    Ce qui se conçoit bien s'énonce clairement, et les mots pour le dire vous viennent aisément. Nicolas Boileau
    Compiler Gcc sous windows avec MinGW
    Coder efficacement en C++ : dans les bacs le 17 février 2014
    mon tout nouveau blog

  6. #6
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2009
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2009
    Messages : 49
    Points : 38
    Points
    38
    Par défaut
    En fait, c'était une question débile de ma part...
    Avec g++, ça marche beaucoup mieux ^^

    Bon j'obtient juste le message d'erreur du au fait que j'ai pas de main, sinon tout roule ^^

    Merci !

  7. #7
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2009
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2009
    Messages : 49
    Points : 38
    Points
    38
    Par défaut
    Nouveau petit problème. J'ai avancé dans le code, le voilà :

    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
     
    /*****
     * Contrainte.cpp 
     ****/
     
    #include <iostream>
    #include "Contrainte.h"
    #include "Recherche.h"
     
    using namespace std;
     
    Contrainte::Contrainte ( Recherche* recherche , int niv )
    {
    	objet = recherche ;
    	niveau = niv ;
    }
     
    bool Contrainte::isValide ()
    {
    	return ( (*objet).getNiveau() >= niveau );
    }

    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
     
    /*****
     * Contrainte.h 
     ****/
     
    #ifndef CONTRAINTE_H
    #define CONTRAINTE_H
     
    class Recherche;
     
    class Contrainte
    {
    	private:
     
    	Recherche* objet ;
    	int niveau ;
     
    	public:
     
    	Contrainte ( Recherche* recherche , int niv );
    	bool isValide();
    };
     
    #endif

    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
    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
     
    /*****
     * Planete.cpp 
     ****/
     
    #include <iostream>
    #include <vector>
    #include <string>
    #include "Contrainte.h"
    #include "Recherche.h"
    #include "Planete.h"
     
    using namespace std;
     
    Planete::Planete ( string NOM , int gal , int sys , int pos )
    {
    	nom = NOM ;
    	galaxie = gal ;
    	systeme_solaire = sys ;
    	position = pos ;
     
    	metal = 0 ;
    	cristal = 0 ;
    	deuterium = 0 ;
     
    	initBatiments();
    	initTechnologies();
     
    	initContraintes();
     
    }
     
    void Planete::initBatiments ()
    {
    	recherches.push_back ( Recherche ( "Mine de métal"					, 1 	, 60 	, 15 	, 0 	) );
    	recherches.push_back ( Recherche ( "Mine de cristal"				, 2 	, 48 	, 24 	, 0 	) );
    	recherches.push_back ( Recherche ( "Synthétiseur de deuterium"		, 3 	, 225 	, 75 	, 0 	) );
    	recherches.push_back ( Recherche ( "Centrale éléctrique solaire"	, 4 	, 75 	, 30 	, 0 	) );
    	recherches.push_back ( Recherche ( "Centrale de fusion"				, 12 	, 900 	, 360 	, 180 	) );
    	recherches.push_back ( Recherche ( "Usine de robots"				, 14 	, 400 	, 120 	, 200 	) );
    	recherches.push_back ( Recherche ( "Usine de nanites"				, 15 	, 1000000 	, 500000 	, 100000 	) );
    	recherches.push_back ( Recherche ( "Chantier spatial"				, 21 	, 400 	, 200 	, 100 	) );
    	recherches.push_back ( Recherche ( "Hangar de métal"				, 22 	, 2000 	, 0 	, 0 	) );
    	recherches.push_back ( Recherche ( "Hangar de cristal"				, 23 	, 2000 	, 1000 	, 0 	) );
    	recherches.push_back ( Recherche ( "Réservoir de deutérium"			, 24 	, 2000 	, 2000 	, 0 	) );
    	recherches.push_back ( Recherche ( "Laboratoire de recherche"		, 31 	, 200 	, 400 	, 200 	) );
    	recherches.push_back ( Recherche ( "Terraformeur"					, 33 	, 0 	, 50000 	, 100000 	) );
    	recherches.push_back ( Recherche ( "Dépôt de ravitaillement"		, 34 	, 20000 	, 40000 	, 0 	) );
    	recherches.push_back ( Recherche ( "Base lunaire"					, 41 	, 20000 	, 40000 	, 20000 	) );
    	recherches.push_back ( Recherche ( "Phalange de capteur"			, 42 	, 20000 	, 40000 	, 20000 	) );
    	recherches.push_back ( Recherche ( "Porte de saut spatial"			, 43 	, 2000000 	, 4000000 	, 2000000 	) );
    	recherches.push_back ( Recherche ( "Silo de missiles"				, 44 	, 20000 	, 20000 	, 1000 	) );
    }
     
    void Planete::initTechnologies ()
    {
    	recherches.push_back ( Recherche ( "Technologie Espionnage"			, 106 	, 200 	, 1000 	, 200 	) );
    	recherches.push_back ( Recherche ( "Technologie Ordinateur"			, 108 	, 0 	, 400 	, 600 	) );
    	recherches.push_back ( Recherche ( "Technologie Armes"				, 109 	, 800 	, 200 	, 0 	) );
    	recherches.push_back ( Recherche ( "Technologie Bouclier"			, 110 	, 200 	, 600 	, 0 	) );
    	recherches.push_back ( Recherche ( "Technologie Protection des vaisseaux spatiaux"	, 111 	, 1000 	, 0 	, 0 	) );
    	recherches.push_back ( Recherche ( "Technologie Energie"			, 113 	, 0 	, 800 	, 400 	) );
    	recherches.push_back ( Recherche ( "Technologie Hyperespace"		, 114 	, 0 	, 4000 	, 2000 	) );
    	recherches.push_back ( Recherche ( "Réacteur à combustion"			, 115 	, 400 	, 0 	, 600 	) );
    	recherches.push_back ( Recherche ( "Réacteur à impulsion"			, 117 	, 2000 	, 4000 	, 600 	) );
    	recherches.push_back ( Recherche ( "Propulsion hyperespace"			, 118 	, 10000 	, 20000 	, 6000 	) );
    	recherches.push_back ( Recherche ( "Technologie Laser"				, 120 	, 200 	, 100 	, 0 	) );
    	recherches.push_back ( Recherche ( "Technologie Ions"				, 121 	, 1000 	, 300 	, 100 	) );
    	recherches.push_back ( Recherche ( "Technologie Plasma"				, 122 	, 2000 	, 4000 	, 1000 	) );
    	recherches.push_back ( Recherche ( "Réseau de recherche intergalactique" , 123 	, 240000 	, 400000 	, 160000 	) );
    	recherches.push_back ( Recherche ( "Technologie Expédition"			, 124 	, 4000 	, 8000 	, 4000 	) );
    }
     
    void Planete::initContraintes ()
    {
    	getRecherche( 12 ).addContrainte( *(getRecherche(   3 )) , 5 );
    	getRecherche( 12 ).addContrainte( *(getRecherche( 113 )) , 3 );
    }
     
    Recherche Planete::getRecherche ( int gid )
    {
    	int i ;
    	for ( i = 0 ; i < recherches.size() ; i++ )
    		if ( recherches[ i ].getGID() == gid )
    			return recherches[ i ];
    	return Recherche( "" , 0 , 0 , 0 , 0 ) ;
    }

    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
     
    /*****
     * Planete.h 
     ****/
     
    #ifndef PLANETE_H
    #define PLANETE_H
     
    #include <vector>
    #include <string>
    #include "Recherche.h"
     
    class Planete
    {
    	private:
     
    	int galaxie ;
    	int systeme_solaire ;
    	int position ;
    	std::string nom ;
     
    	int metal ;
    	int cristal ;
    	int deuterium ;
     
    	std::vector<Recherche> recherches ;
     
    	public:
     
    	Planete ( std::string NOM , int gal , int sys , int pos );
    	void initBatiments ();
    	void initTechnologies ();
    	void initContraintes ();
    	Recherche getRecherche ( int gid );
     
    };
     
    #endif

    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
     
    /*****
     * Recherche.cpp 
     ****/
     
    #include <iostream>
    #include <vector>
    #include <string>
    #include "Contrainte.h"
    #include "Recherche.h"
     
    using namespace std;
     
    Recherche::Recherche( std::string NOM , int GID , int METAL , int CRISTAL , int DEUT )
    {
    	nom = NOM ;
    	gid = GID ;
    	metal = METAL ;
    	cristal = CRISTAL ;
    	deuterium = DEUT ;
    	niveau = 0 ;
    }
     
    int Recherche::getGID()
    {
    	return gid ;
    }
     
    int Recherche::getNiveau()
    {
    	return niveau ;
    }
     
    void Recherche::setNiveau( int niv )
    {
    	niveau = niv ;
    }
     
    void Recherche::addContrainte ( Recherche* objet , int niv )
    {
    	contraintes.push_back( Contrainte( objet , niv ) );
    }

    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
     
    /*****
     * Recherche.h 
     ****/
     
    #ifndef RECHERCHE_H
    #define RECHERCHE_H
     
    #include <vector>
    #include <string>
    #include "Contrainte.h"
     
     
    class Recherche
    {
    	protected:
     
    	std::string nom ;
    	int gid ;
    	int metal ;
    	int cristal ;
    	int deuterium ;
    	int niveau ;
    	std::vector<Contrainte> contraintes ;
     
    	public:
     
    	Recherche( std::string NOM , int GID , int METAL , int CRISTAL , int DEUT );
    	int getGID ();
    	int getNiveau ();
    	void setNiveau ( int niv );
    	void addContrainte ( Recherche* objet , int niv );
    };
     
    #endif

    Mais j'ai une satanée erreur

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    Planete.cpp: In member function ‘void Planete::initContraintes()’:
    Planete.cpp:71: error: no match foroperator*’ in ‘*Planete::getRecherche(int)(3)’
    Planete.cpp:72: error: no match foroperator*’ in ‘*Planete::getRecherche(int)(113)
    Si vous pouvez m'aider, je serais bien content

  8. #8
    Membre chevronné
    Avatar de poukill
    Profil pro
    Inscrit en
    Février 2006
    Messages
    2 155
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 2 155
    Points : 2 107
    Points
    2 107
    Par défaut
    Planete::getRecherche(int) est une fonction membre qui renvoie un Recherche.
    Pourquoi vouloir déréférencer ? Tu veux prendre l'adresse on dirait...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    void Planete::initContraintes ()
    {
    	getRecherche( 12 ).addContrainte( &(getRecherche(   3 )) , 5 );
    	getRecherche( 12 ).addContrainte( &(getRecherche( 113 )) , 3 );
    }

  9. #9
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2009
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2009
    Messages : 49
    Points : 38
    Points
    38
    Par défaut
    Oui oui, je veux prendre l'adresse, mais j'ai déjà tenté ça. Bon après c'est peut-être la bonne solution ( Un peu naze, je réfléchi plus très bien )

    En tout cas, je reçois toujours un message d'erreur lors de la compilation
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    Planete.cpp: In member function ‘void Planete::initContraintes()’:
    Planete.cpp:71: warning: taking address of temporary
    Planete.cpp:72: warning: taking address of temporary
    /usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o: In function `_start':
    /build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf/start.S:115: undefined reference to `main'
    collect2: ld returned 1 exit status
    (La dernière partie, due à l'absence de main^^)

    Mais mon but est bien de récupérer l'adresser ^^

  10. #10
    Membre éclairé

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2007
    Messages
    373
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Santé

    Informations forums :
    Inscription : Juin 2007
    Messages : 373
    Points : 764
    Points
    764
    Par défaut
    Ton code compile (si tu écris le main), mais ne fonctionnera certainement pas.
    En effet, ta fonction getRecherche() retourne une copie temporaire d'une recherche de ta planète.
    Du coup :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    void Planete::initContraintes ()
    {
    	getRecherche( 12 ).addContrainte( &(getRecherche(   3 )) , 5 );
    	getRecherche( 12 ).addContrainte( &(getRecherche( 113 )) , 3 );
    }
    Cette fonction n'a aucun effet, car getRecherche(12) retourne une Recherche temporaire qui sera détruite à la sortie de la fonction. Tu modifies cette copie temporaire, mais pas la recherche en elle même.
    De plus, les adresses de getRecherche(3) et getRecherche(113) sont les adresses de variable temporaires encore une fois, qui seront elles aussi détruites à la sortie de la fonction. Elles sont donc invalides.

    Pour que tout fonctionne, il faut que tu changes ta fonction getRecherche() pour qu'elle retourne un pointeur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    Recherche* Planete::getRecherche ( int gid )
    {
    	int i ;
    	for ( i = 0 ; i < recherches.size() ; i++ )
    		if ( recherches[ i ].getGID() == gid )
    			return &recherches[ i ]; 
    	return NULL;
    }

  11. #11
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Points : 13 017
    Points
    13 017
    Par défaut
    Une référence en retour peut suffire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    Recherche& Planete::getRecherche ( int gid )
    {
    	int i ;
    	for ( i = 0 ; i < recherches.size() ; i++ )
    		if ( recherches[ i ].getGID() == gid )
    			return recherches[ i ]; 
    	throw std::invalid_argument("bad GID");
    }

  12. #12
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2009
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2009
    Messages : 49
    Points : 38
    Points
    38
    Par défaut
    Merci beaucoup de ton aide, ça m'a beaucoup aidé.

    En modifiant en suite mon code comme cela

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    void Planete::initContraintes ()
    {
    	(*(getRecherche( 12 ))).addContrainte( (getRecherche(   3 )) , 5 );
    	(*(getRecherche( 12 ))).addContrainte( (getRecherche( 113 )) , 3 );
    }
    Et les modifs que tu m'as donné, ça compile, bien ^^

Discussions similaires

  1. Réponses: 1
    Dernier message: 19/04/2012, 12h31
  2. Réponses: 8
    Dernier message: 29/07/2009, 12h22
  3. Push_back() de std::vector
    Par tir0nik dans le forum SL & STL
    Réponses: 10
    Dernier message: 03/05/2009, 19h26
  4. vector et push_back qui ne fait pas bouge le size
    Par skerdreux dans le forum SL & STL
    Réponses: 7
    Dernier message: 24/04/2008, 15h34
  5. [Vector] Copie des éléments lors d'un push_back() ?
    Par 5kippy dans le forum SL & STL
    Réponses: 4
    Dernier message: 22/11/2007, 11h11

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo