-4

I try since a few day to make this little code but it doesn't work. I see a lot of question about this problem but i didn't find an answer to mine.

Here is the code for Voiture.cpp :

#include <iostream>
using namespace std;
#include <string.h>
#include "modele.h"

Voiture::Voiture()
{
Nom=NULL;
setNom("Default");
VoitChoix=Modele();
cout << "COnstructeur default" << endl;
}

Voiture::Voiture(const char* N,const Modele V)
{
Nom=NULL;
setNom(N);
setModele(V);
cout << "COnstructeur initialisation" << endl;
}

Voiture::Voiture(const Voiture& V)
{
Nom=NULL;
setNom(V.getNom());
setModele(V.getModele());
cout << "COnstructeur copie" << endl;
}
Voiture::~Voiture()
{
if(Nom)
{
    cout << "Voiture : Destruction de" << Nom << endl;
    delete [] Nom;
}
}    

Here is the code for Modele.cpp :

#include <iostream>
#include <string.h>
using namespace std;
#include "modele.h"

Modele::Modele()
{
Nom=NULL;
setNom("Default");
Puissance=0;
Diesel=true;
PrixDeBase=0;
cout << "COnstructeur default" << endl;
}
Modele::Modele(const char* N,const int P,const bool D,const float PDB)
{
Nom=NULL;
setNom(N);
setPuissance(P);
setDiesel(D);
setPrixDeBase(PDB);
cout << "COnstructeur initialisation" << endl;
}
Modele::Modele(const Modele& M)
{
Nom=NULL;
setNom(M.getNom());
setPuissance(M.getPuissance());
setDiesel(M.isDiesel());
setPrixDeBase(M.getPrixDeBase());
cout << "COnstructeur copie" << endl;
}

Modele::~Modele()
{
if(Nom)
{
    cout << "Modele: Destruction de" << Nom << endl;
    delete [] Nom;
}
}

Here is the code for main.cpp :

int main()
{
cout << "(1) ***** Test du constructeur par defaut de Voiture *****" << endl;
{
Voiture voiture;
voiture.Affiche();
}
}

I don't put all the code, just where i have the problem.

Thanks ! :(

  • You should also provide the class definitions, so we can at least see the data types of class's data members. – antred Oct 16 '14 at 08:06
  • I *think* I've spotted the problem (or at least one of the problems) in your code. In the copy constructor of your Modele class, you assign the value of the source instance's Nom pointer to your new instance's Nom pointer (at least I suspect that that's what your setNom method does). After this, the Nom pointers of both the source instance and the new instance will point to the same array and, when they're destroyed, will attempt to delete the same array. You could fix this by making your Nom member of type std::string instead of using a dynamically allocated character array. – antred Oct 16 '14 at 08:12
  • Please edit your code so that it is properly indented. Everything is flushed to the left margin, making it hard to read your code. – PaulMcKenzie Oct 16 '14 at 08:36

1 Answers1

1

One obvious problem is that you're missing a user-defined assignment operator:

VoitChoix=Modele();

This calls the assignment operator, not copy constructor. Since you do not have a user-defined assignment operator for Modele, then you will have issues on destruction of VoitChoix. More specifically, you are assigning all of the values that Modele() has created to VoitChoix.

So you have two instances that have the same pointer value for Nom. When the temporary Modele() goes out of scope, it will call the destructor, thus deleting Nom. When VoitChoix goes out of scope, it will attempt to delete the same pointer value for Nom. Thus the double delete error.

The user defined assignment operator for Modele would have the following signature:

Modele& operator=(const Modele&);

You will need to implement this function before going any further. This can be done easily using the copy/swap idiom: What is the copy-and-swap idiom?

Also, please follow the rule of three when creating your class: What is The Rule of Three?

Community
  • 1
  • 1
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45