CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2007
    Posts
    62

    Unable do dynamically allocate an array (basic stuff)

    Alright so this is pretty basic stuff, but I just can't get it to work.
    I have 3 classes:

    Equipe:
    Code:
    #ifndef EQUIPE_H
    #define EQUIPE_H
    
    #include <string>
    
    using namespace std;
    
    class Equipe
    {
    public:
      Equipe(short no, Personne* membre1, Personne* membre2); 
      ~Equipe();
    
      // Méthode pour afficher le numéro et  les noms des deux membres de l'équipe
      void afficher();  
    
    private:
      // Ajouter les attributs privés
    };
    
    #endif
    Personne:
    Code:
    #ifndef PERSONNE_H
    #define PERSONNE_H
    
    #include <string>
    
    using namespace std;
    
    class Personne
    {
    public:
      Personne(const string nom = "" , const short age = 0);
       
      string obtenirNom() const;
      short obtenirAge() const;
      
      void modifierNom(string nom);
      void modifierAge(short age);
    
    private:
      // Ajouter les attributs privés
    };
    
    #endif
    And Personnel:
    Code:
    #ifndef PERSONNEL_H
    #define PERSONNEL_H
    
    #include "personne.h"
    #include "string"
    
    using namespace std;
    
    const short CAPACITE_INITIALE = 2;
    
    class Personnel
    {
    public:
      Personnel();
      ~Personnel();
      
      void       ajouterPersonne(string nom, short age);
      Personne*  obtenirPersonne(string nom);
      void       afficher();
      
    private:
      short  nombrePersonnes_;    // Nombre de personnes contenues dans le tableau dynamique
      short  capacite_;           // Nombre maximal de personnes que le tableau dynamique 
                                  // peut contenir
      Personne** lesPersonnes_;   // Tableau dynamique de pointeurs
    };
    
    #endif
    This is the implementation of the class Personnel:
    Code:
    #include "personnel.h"
    #include "string"
    #include <iostream>
    
    using namespace std;
    
    Personnel::Personnel()
    {
    	// On crée un tableau dynamique dont la taille correspond au nombre de personnes
    	// Attention: il s'agit d'un tableau dynamiques qui contiendra des pointeurs sur 
    	// des personnes
    	// On fixe la capacité initiale du tableau Ã* deux personnes
    	capacite_ = CAPACITE_INITIALE;
    	lesPersonnes_ = new Personne*[capacite_];
    	// On initialise les autres attributs
    	nombrePersonnes_ = 0;
    }
    
    Personnel::~Personnel(void)
    {
    	// A faire...
    }
    
    void  Personnel::ajouterPersonne(string nom, short age)
    {
    	// Si le tableau est plein, on le remplace par un autre tableau dont la capacite
    	// est le double
    	if (nombrePersonnes_ == capacite_) {
    		capacite_ = capacite_ * 2;
    	}
    	// Ajouter la personne supplémentaire
    	lesPersonnes_[nombrePersonnes_] = new Personne(nom, age);
    	++nombrePersonnes_;
    }
    
    Personne*  Personnel::obtenirPersonne(string nom)
    {
      Personne* personneRecherchee = 0;
    
      // Compléter l'implémentation...
      
      return personneRecherchee;
    }
    
    void  Personnel::afficher()
    {
      // Méthode Ã* implémenter...
    }
    And this is my main:
    Code:
    #include <iostream>
    #include "personnel.h"
    #include "personne.h"
    #include "equipe.h"
    
    using namespace std;
    
    int main()
    {
    	string nom;
    	short age;
    	// Creer un objet de type Personnel
    	Personnel lePersonnel;
    	// Ajouter deux personnes au personnel
    	cout << "Veuillez entrer le nom de la premiere personne" << endl;
    	cin >> nom;
    	cout << "Veuillez entrer lage de la premiere personne" << endl;
    	cin >> age;
    	lePersonnel.ajouterPersonne(nom, age);
    	cout << "Veuillez entrer le nom de la deuxieme personne" << endl;
    	cin >> nom;
    	cout << "Veuillez entrer lage de la deuxieme personne" << endl;
    	cin >> age;
    	lePersonnel.ajouterPersonne(nom, age);
      // Afficher la liste des membres du personnel
    
      // Ajouter une personne au personnel
    
      // Afficher la liste des membres du personnel
    
      // Creer une équipe avec deux personnes choisies parmi le personnel
    
      // Afficher le numéro et les noms des membres de l'équipe
    
      return 0;
    }
    What I'm trying to do is to get the user to enter a NAME (nom) and AGE (age) to store a person (Personne) in a dynamic array. I just can't get the dynamic table working... this thing won't even compile. I think the problem might be in my "Personnel" implementation... Please help!
    Last edited by Ghettobusta; January 28th, 2009 at 06:23 PM.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Unable do dynamically allocate an array (basic stuff)

    Are we supposed to guess at the compiler errors?

    And incidentally using a vector<Person> might be easier than trying to allocate the array manually.

    I do notice that you don't appear to actually be doing any reallocation when you double "capacity".
    Last edited by Lindley; January 28th, 2009 at 06:27 PM.

  3. #3
    Join Date
    Oct 2007
    Posts
    62

    Re: Unable do dynamically allocate an array (basic stuff)

    Quote Originally Posted by Lindley View Post
    Are we supposed to guess at the compiler errors?
    Sorry.

    1>personnel.obj : error LNK2019: unresolved external symbol "public: __thiscall Personne::Personne(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,short)" (??0Personne@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@F@Z) referenced in function "public: void __thiscall Personnel::ajouterPersonne(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,short)" (?ajouterPersonne@Personnel@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@F@Z)
    1>C:\Users\Alex\Desktop\POLY09\INF1010\TD2\td2_1364883\td2_1364883\Debug\td2_1364883.exe : fatal error LNK1120: 1 unresolved externals

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Unable do dynamically allocate an array (basic stuff)

    Oh, so it compiles just fine, but it won't link.

    It's complaining that you haven't implemented this constructor:
    Code:
      Personne(const string nom = "" , const short age = 0);

  5. #5
    Join Date
    Oct 2007
    Posts
    62

    Re: Unable do dynamically allocate an array (basic stuff)

    Quote Originally Posted by Lindley View Post
    Oh, so it compiles just fine, but it won't link.

    It's complaining that you haven't implemented this constructor:
    Code:
      Personne(const string nom = "" , const short age = 0);
    Right... I haven't. I'll get to that right away then. Thanks!
    Oh and if I am using this method to code this, it's because I have to do exactly what was asked.

  6. #6
    Join Date
    Oct 2007
    Posts
    62

    Re: Unable do dynamically allocate a table

    I do have to say... I don't know where to start to implement
    Code:
    Personne(const string nom = "" , const short age = 0);
    I am completly lost... am I on the good track with what I have at least?

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Unable do dynamically allocate a table

    The constructor should be a simple matter of assigning the arguments to their appropriate corresponding private members.

    Can't say how well you're coming without seeing the full class definition for Person, at the least. I can tell you your array resizing code isn't done.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured