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++

Vue hybride

Message pr�c�dent Message pr�c�dent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    �tudiant
    Inscrit en
    F�vrier 2009
    Messages
    49
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 35

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : F�vrier 2009
    Messages : 49
    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 : 41
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activit� : D�veloppeur informatique

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

  3. #3
    Membre exp�riment�
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    188
    D�tails du profil
    Informations personnelles :
    �ge : 38
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 188
    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 �clair�
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    318
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 318
    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
    Avatar de koala01
    Homme Profil pro
    aucun
    Inscrit en
    Octobre 2004
    Messages
    11 644
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 53
    Localisation : Belgique

    Informations professionnelles :
    Activit� : aucun

    Informations forums :
    Inscription : Octobre 2004
    Messages : 11 644
    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
    Membre averti
    Homme Profil pro
    �tudiant
    Inscrit en
    F�vrier 2009
    Messages
    49
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 35

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : F�vrier 2009
    Messages : 49
    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
    Membre averti
    Homme Profil pro
    �tudiant
    Inscrit en
    F�vrier 2009
    Messages
    49
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 35

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : F�vrier 2009
    Messages : 49
    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 Expert
    Avatar de poukill
    Profil pro
    Inscrit en
    F�vrier 2006
    Messages
    2 155
    D�tails du profil
    Informations personnelles :
    �ge : 41
    Localisation : France

    Informations forums :
    Inscription : F�vrier 2006
    Messages : 2 155
    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
    Membre averti
    Homme Profil pro
    �tudiant
    Inscrit en
    F�vrier 2009
    Messages
    49
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 35

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : F�vrier 2009
    Messages : 49
    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 chevronn�

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

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

    Informations forums :
    Inscription : Juin 2007
    Messages : 373
    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
    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
    Membre averti
    Homme Profil pro
    �tudiant
    Inscrit en
    F�vrier 2009
    Messages
    49
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 35

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : F�vrier 2009
    Messages : 49
    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. 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