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 :

classes imbriquées et pointeurs


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 classes imbriquées et pointeurs
    Bonjour à tous.

    Je tente de créer deux classes : Recherche et Contrainte.
    Chaque Recherche possède des contraintes et chaque contrainte est en fait un pointeur sur une recherche.

    Pour plus de détail, voici mes classes :


    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
    31
     
    #ifndef RECHERCHE_H
    #define RECHERCHE_H
     
    #include <vector>
    #include <string>
    #include "Contrainte.h"
     
    class Contrainte;
     
    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
    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 );
    }
    et 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
     
    #include "Recherche.h"
     
    class Contrainte
    {
    	private:
     
    	Recherche* objet ;
    	int niveau ;
     
    	public:
     
    	Contrainte ( Recherche* recherche , int niv );
    	bool isValide();
    };
     
    #endif
    Le problème est que j'ai quelques problèmes avec (je pense) les pointeurs.

    À la compilation avec gcc j'obtient une erreur assez illisible :
    /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/cclAY4Ci.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/cclAY4Ci.o.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
    /tmp/cckuTJyD.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/cckuTJyD.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/cckuTJyD.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/cckuTJyD.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/cckuTJyD.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/cckuTJyD.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/cckuTJyD.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/cckuTJyD.o.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
    /tmp/cckuTJyD.o.eh_frame+0x4b): undefined reference to `__gxx_personality_v0'
    collect2: ld returned 1 exit status
    Pouvez vous m'orienter vers une solution. Merci d'avance.

  2. #2
    Membre éclairé

    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    717
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 717
    Points : 858
    Points
    858
    Par défaut
    Il s'agit certainement d'un problème de compilation, peux-tu nous montrer ton Makefile ou la ligne de commande que tu utilise pour compiler ?

  3. #3
    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
    Sachant que j'ai que 4 fichier dans le dossier, les 2 .cpp et les 2 .h

  4. #4
    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
    Bonjour,
    Lis jusqu'au bout la F.A.Q. : Comment créer 2 classes qui font référence l'une à l'autre ?
    En l'occurrence, c'est Contrainte qui devrait utiliser une déclaration anticipée de Recherche ... et ne pas inclure l'en-tête complet dans sa déclaration.
    Recherche.h
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    #include "contrainte.h"
    Contrainte.h
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    class Recherche;
    class contrainte 
    {...
    Contrainte.cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    #include "contrainte.h"
    #include "recherche.h"

  5. #5
    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, j'ai fait comme 3DArchi m'a conseillé, j'ai modifié les classes, mais le problème d'inclusion reste entier.
    Je vous redonne les classes et le message d'erreur.


    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
    21
    22
     
    #ifndef CONTRAINTE_H
    #define CONTRAINTE_H
     
    #include "Recherche.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( new 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
    #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
    Et voici le resultat du gcc

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    In file included from Contrainte.h:4,
                     from Contrainte.cpp:2:
    Recherche.h:19: error: ‘Contrainte’ was not declared in this scope
    Recherche.h:19: error: template argument 1 is invalid
    Recherche.h:19: error: template argument 2 is invalid
    In file included from Contrainte.h:4,
                     from Recherche.cpp:4:
    Recherche.h:19: error: ‘Contrainte’ was not declared in this scope
    Recherche.h:19: error: template argument 1 is invalid
    Recherche.h:19: error: template argument 2 is invalid
    Recherche.cpp: In member function ‘void Recherche::addContrainte(Recherche*, int)’:
    Recherche.cpp:31: error: request for member ‘push_back’ in ‘((Recherche*)this)->Recherche::contraintes’, which is of non-class type ‘int
    Bon le problème n'a pas l'air d'être seulement un problème d'inclusion, mais ce problème reste entier.

  6. #6
    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
    enleve #include "Recherche.h" dans Contrainte.h

  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
    Merci ^^

  8. #8
    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
    Citation Envoyé par zbahoui Voir le message
    Merci, j'ai fait comme 3DArchi m'a conseillé
    faut croire que tu n'avais pas vraiment lu ce que j'avais dit...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [C#]Remonter des événements dans des classes imbriquées
    Par Kcirtap dans le forum Windows Forms
    Réponses: 9
    Dernier message: 14/12/2013, 12h43
  2. Pointeur de fonction et classes imbriquées
    Par XAMLdev dans le forum Langage
    Réponses: 14
    Dernier message: 08/08/2013, 11h14
  3. Classe imbriquée
    Par dj.motte dans le forum C++
    Réponses: 10
    Dernier message: 13/04/2006, 07h34
  4. classes imbriquées
    Par smedini dans le forum C++
    Réponses: 10
    Dernier message: 10/12/2005, 13h16
  5. Intérêt des classes imbriquées ?
    Par elitost dans le forum Langage
    Réponses: 5
    Dernier message: 21/10/2005, 09h30

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