-
January 28th, 2009, 06:16 PM
#1
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.
-
January 28th, 2009, 06:24 PM
#2
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.
-
January 28th, 2009, 06:25 PM
#3
Re: Unable do dynamically allocate an array (basic stuff)
Originally Posted by Lindley
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
-
January 28th, 2009, 06:29 PM
#4
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);
-
January 28th, 2009, 06:32 PM
#5
Re: Unable do dynamically allocate an array (basic stuff)
Originally Posted by Lindley
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.
-
January 28th, 2009, 06:36 PM
#6
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?
-
January 28th, 2009, 07:18 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|