Problème constructeur par défaut
Bonjour,
je débute en C++ sur un projet de répertoire avec des classes.
j'ai une erreur de compilation que j'ai du mal à comprendre : main.cpp:25:16: error: use of deleted function ‘Repertoire::Repertoire()’
Après lecture sur différents forums, je comprends qu'il s'agit d'une problème au niveau du constructeur par défaut et notamment à l'initialisation des attributs utilisés par le constructeur par défaut
J'essaie donc d'initialiser les attributs de Repertoire et j'ai un nouveau message d'erreur sur la ligne Contact tab[taille]={0} : repertoire.h:32:31: error: could not convert ‘0’ from ‘int’ to ‘Contact’ que je ne sais pas comment résoudre
J'ai essayé Contact tab[taille]={nullptr} et Contact tab[taille]=0...
Je suis un peu largué...
main.cppp
Code:
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
|
#include <cstdlib>
#include <iostream>
#include <string>
#include "interface.h"
#include "repertoire.h"
#include "contact.h"
using namespace std;
int main(int argc, char** argv) {
Repertoire r;
int choix = 0;
//tant que l'utilisateur ne quitte pas le programme on affiche le menu
while(choix!=9){
menu();
std::cout<<"Votre choix ? ";
std::cin >> choix ;
traiterChoix(choix,r);
};
return 0;
} |
interface.cpp
Code:
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
|
#include <cstdlib>
#include <iostream>
#include <string>
#include "interface.h"
#include "repertoire.h"
#include "contact.h"
using namespace std;
void menu(){
std::cout << "1 : Rentrer un nouveau contact" << endl;
std::cout << "2 : Afficher mes contacts" << endl;
std::cout << "3 : Rechercher un contact" << endl;
std::cout << "9 : Quitter" << endl;
}
void traiterChoix(int choix, Repertoire r){
string nom;
string numero;
switch(choix)
{
case 1 :
//Saisies utilisateurs
std::cout << "Veuillez saisir un nom" << endl ;
std::cin >> nom;
std::cout << "Veuillez saisir un numero" << endl ;
std::cin >> numero;
//Création du contact
Contact c(nom,numero);
//Ajout du contact au répertoire
if(r.AjouterContact(c))
std::cout << "Contact ajouté" << endl ;
else
std::cout << "Votre répertoire est complet" << endl ;
break;
/*case 2 :
r.repertoire::Afficher();
break;
case 3 :
std::cout << "Veuillez saisir un nom" << endl ;
std::cin >> nom;
if(r.RechercherContact(nom)==nullptr)
std::cout << "Aucun contact correspondant à ce nom" << endl ;
else
std::cout << r.RechercherContact(nom).nom << " (" << r.RechercherContact(nom).numero << ")" << endl;
break;
case 9 :
break;
default :
break;*/
}
} |
interface.h
Code:
1 2 3 4 5 6 7 8 9
|
#ifndef INTERFACE_H
#define INTERFACE_H
#include "repertoire.h"
void menu();
void traiterChoix(int,Repertoire);
#endif /* INTERFACE_H */ |
repertoire.cpp
Code:
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
|
#include <cstdlib>
#include <iostream>
#include <string>
#include "repertoire.h"
#include "contact.h"
using namespace std;
//ajout d'un nouveau contact
bool Repertoire::AjouterContact(Contact& c)//passage par référence du compteur car paramètre entrée/sortie
{
//si le répertoire est complet, on informe l'utilisateur
if(compteur>=Repertoire::taille){
return false;
}
//sinon, on peut saisir un nouveau contact
else {
//insertion du contact téléphonique au premier emplacement disponible du répertoire
tab[compteur]=c;
compteur++;
return true;
}
}
//affichage de la liste des contacts entrés dans le répertoire
void Repertoire::Afficher(){
for(int i=0; i<compteur; i++){
std::cout << tab[i].nom << " (" << tab[i].numero << ")" << endl;
}
}
//rechercher un contact à partir de son nom
//renvoie l'adresse (pointeur*) ou nullptr si le contact n'existe pas
Contact* Repertoire::RechercherContact(string nom){
bool trouve = false;//variable de statut de la recherche de contact
for(int i=0;i<compteur;i++){
if(tab[i].nom == nom) {
trouve = true;
return true;
}
}
if(!trouve){
return nullptr;
}
} |
repertoire.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
class Repertoire{
public:
//methodes
bool AjouterContact(const Contact& c);
void Afficher();
Contact* RechercherContact(const std::string& nom); //on ne modifie par le parametre et on le passe par référence pour éviter clonage
//on utilise un pointeur qui peut renvoyer NULL si aucun contact dans le répertoire
private:
static const int taille = 5;
Contact tab[taille]={0};
int compteur=0;
};
#endif /* REPERTOIRE_H */ |
contact.cpp
Code:
1 2 3 4 5 6 7 8 9 10
|
#include <string>
#include <iostream>
#include "contact.h"
Contact::Contact(std::string nom,std::string numero) {
nom=nom;
numero=numero;
//std::cout << "appel constructeur sans param" << std::endl;
} |
contact.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#ifndef CONTACT_H
#define CONTACT_H
#include <string>
class Contact{
public:
//Constructeur
Contact(std::string nom, std::string numero);
std::string nom;
std::string numero;
};
#endif /* CONTACT_H */ |