N'arrive pas à utiliser ma fct affiche!!

cs_nelly77 Messages postés 6 Date d'inscription vendredi 27 mars 2009 Statut Membre Dernière intervention 26 novembre 2012 - 27 mars 2009 à 18:46
cs_Chouchou182 Messages postés 252 Date d'inscription vendredi 13 juin 2003 Statut Membre Dernière intervention 25 avril 2011 - 30 mars 2009 à 18:58
#include
using namespace std;

class CPersonne
{
protected:
    char* nom;
public:
    CPersonne(char* name);
    void setNom(char* name);

    void affiche();
};

CPersonne::CPersonne( char*name)

{

this->setNom(name);

}

void CPersonne::setNom( char*name)
{

    if(name)
{
    this->nom=new char[strlen(name)];
    strcpy(this->nom,name);
}

    else
    nom=NULL;
}

void CPersonne::affiche()

{
    cout<< "Nom:"<<this->nom<<endl;

}

int main(int argc,char*argv[])
{
CPersonne nom;
nom.affiche();
return 0;
}

ca m gener des erreurs alors si vs savez pourquoi j  p rien afficher merci d'avance......

3 réponses

cs_Chouchou182 Messages postés 252 Date d'inscription vendredi 13 juin 2003 Statut Membre Dernière intervention 25 avril 2011 1
27 mars 2009 à 19:18
Bonjour,

Lorsque du déclares un objet, un constructeur est appelé (pour construire l'objet). Si tu ne précises pas quel constructeur utiliser, c'est le constructeur par défaut, s'il existe, qui est choisi.
Dans ton cas, à la ligne
CPersonne nom;
tu demandes de construire un objet CPersonne en employant le constructeur par défaut. Or, il n'y a pas de constructeur par défaut, puisque tu as défini ton propre constructeur CPersonne(char*).
Il te faut donc appeler le constructeur avec un argument. Par exemple:
CPersonne nom("Nelly77");

Bonne prog,
0
cs_nelly77 Messages postés 6 Date d'inscription vendredi 27 mars 2009 Statut Membre Dernière intervention 26 novembre 2012
30 mars 2009 à 16:21
merci pour ta reponse en fait j'essai ca comme exercice parceque ce que je  veux vraiment faire c'est celui la: mais j'ai une tonne d'erreurs jarrive à afficher ni l nom ni les autres attributs de ma classe si tu peu essayer ce serai sympa.
#include
using namespace std;

class Cproduit
{
protected:

    char* nomProduit;
    int* etatStock;
    int* dureeFab;
    int* delaiLiv;

public:
    Cproduit(const char*nom, int*etat,const int*duree,const int*delai);
    void setNom(const char*nom);
    void setEtat( int*etat);
    void setDuree(const int*duree);
    void setDelai(const int*delai);
    void affiche();
};

Cproduit::Cproduit(const char*nom, int*etat,const int*duree,const int*delai)

{

this->setNom(nomProduit);
this->setEtat(etatStock);
this->setDuree(dureeFab);
this->setDelai(delaiLiv);
}

void Cproduit::setNom(const char*nom)
{

    if(nom)
{
    this->nomProduit=new char[strlen(nom)];
    strcpy(this->nomProduit,nom);
}

    else
    nomProduit=NULL;
}

void Cproduit::setEtat( int*etat)
{

    if(etat)
{
    this->etatStock=etat;
}

    else this->etatStock=NULL;
}

void Cproduit::setDuree(const int*duree)
{

    if(duree)
{
    this->dureeFab;
}

    else this->dureeFab=NULL;
}

void Cproduit::setDelai(const int*delai)
{

    if(delai)
{
    this->delaiLiv;
}

    else this->delaiLiv=NULL;
}

void Cproduit::affiche()

{
    cout<< "nomProduit:"<<this->nomProduit<<endl;
    cout<< "etatStock:"<<etatStock<<endl;
    cout<< "dureefab:"<<dureeFab<<endl;
    cout<< "delailiv:"<<delaiLiv<<endl;
}

int main(int argc,char*argv[])

{
Cproduit nomProduit("stylo");
Cproduit etatStock(5);
nomproduit.affiche();
etatStock.affiche();

  return 0;

}
0
cs_Chouchou182 Messages postés 252 Date d'inscription vendredi 13 juin 2003 Statut Membre Dernière intervention 25 avril 2011 1
30 mars 2009 à 18:58
Salut,
Ci-dessous une version «qui compile» de ton code.
Quelques remarques:
1/ Les membres de type int de ta classe n'ont, me semble-t-il, pas besoin d'être des pointeurs.
2/ Il est bon de libérer dans un destructeur les ressources allouées dynamiquement dans le constructeur par exemple (à tout new son delete).
3/ Inutile d'employer this à toutes les sauces.

#include
#include<cstring>
using namespace std;

class Cproduit
{
    char* nomProduit;
    int etatStock;
    int dureeFab;
    int delaiLiv;

public:
    Cproduit(const char*nom, int etat=0,int duree=0,int delai=0);
    ~Cproduit();
    void setNom(const char*nom);
    void setEtat(int etat);
    void setDuree(int duree);
    void setDelai(int delai);
    void affiche();
};

Cproduit::Cproduit(const char*nom, int etat,int duree,int delai)
{
  setNom(nom);
  setEtat(etat);
  setDuree(duree);
  setDelai(delai);
}

Cproduit::~Cproduit() {
  cout << "objet "<< nomProduit << " détruit" << endl;
  if (nomProduit != NULL) {
    delete [] nomProduit ;
    nomProduit = NULL ;
  }
}

void Cproduit::setNom(const char*nom)
{
  if(nom) {
    this->nomProduit=new char[strlen(nom)];
    strcpy(this->nomProduit,nom);
  }
  else
    nomProduit=NULL;
}

void Cproduit::setEtat(int etat)
{
  etatStock=etat;
}

void Cproduit::setDuree(int duree)
{
  dureeFab=duree;
}

void Cproduit::setDelai(int delai)
{
  delaiLiv = delai;
}

void Cproduit::affiche()
{
  cout<< "nomProduit: "<<nomProduit<<endl;
  cout<< "etatStock: "<<etatStock<<endl;
  cout<< "dureefab: "<<dureeFab<<endl;
  cout<< "delailiv: "<<delaiLiv<<endl;
}

int main()
{
  Cproduit p1("stylo", 5), p2("crayon");
  p1.affiche();
  p2.affiche();

  return 0;
}
0
Rejoignez-nous