Erreur de segmentation (débutant)

Fermé
Vicoo - 27 oct. 2012 à 16:28
 Vicoo - 27 oct. 2012 à 20:05
Bonjour,

j'ai eu erreur que j'arrive pas pourquoi :

http://www.casimages.com/img.php?i=121027043843387625.png

int main() {
  Personnage * p[3];
  p[0] = new hero("C","Rambo", 100, false, 4, 10, 0, 0, 5, 5);
  p[1] = new hero("C","mini", 100, false, 4, 10, 0, 0, 5, 5);
  p[2] = new hero("C","mechant", 100, false, 4, 10, 0, 0, 5, 5);
 
  cout << "OK" << endl;

 Grille ferme(p);

for(int i=0 ; i<3 ; i++)
{
     delete  p[i];
}
  return 0;
}


class Grille{
  Personnage* g[5][5];

 public:

  Grille(Personnage * p[]);
  ~Grille(){delete[] g;};
bool vide(int x, int y);

  void affiche();
  string get_perso(int x,int y); 

};




Et le destructeur personnage:
Personnage::~Personnage() {cout<<"Personnage détruit"<<endl;}



Une idée d'ou vient le problème ?
'lerreur est sur l'image voir sur le lien' :)


Merci a tous

5 réponses

doctormad Messages postés 430 Date d'inscription mercredi 28 novembre 2007 Statut Membre Dernière intervention 2 avril 2015 99
27 oct. 2012 à 17:53
Le problème vient visiblement de la classe Personnage ou de son utilisation.
0
tu pourrais développer stp ?

je pense que ca vient de mon delete je dois mal supprimer la mémoire alloué, ais je ne vois pas comment faire autrement, des idées ?

up please !
0
doctormad Messages postés 430 Date d'inscription mercredi 28 novembre 2007 Statut Membre Dernière intervention 2 avril 2015 99
27 oct. 2012 à 18:36
Eh bien déjà si tu peux publier la classe en question ça pourrait aider.
0
Grille.cpp:

#include"Grille.h"

Grille :: Grille(Personnage* p[]){
    for(int i=0;i<5;++i){
      for(int j=0;j<5;++j){
	g[i][j]=0;
      }
    }
    srand(time(NULL));
    int x,y; // les coord
    for(int k = 0; k<3; ++k){
      do{
    x = rand()%5;
    y = rand () %5;
      }while(!vide(x,y));
      g[x][y] =p[k];
    }cout<<"grille cree"<<endl;
}


Grille.h:
#ifndef GRILLE
#define GRILLE




#include "Personnage.h"
#include "Canard.h"

#include <time.h>
#include <stdio.h>
#include <stdlib.h>


class Grille{
  Personnage* g[5][5];

 public:

  Grille(Personnage * p[]);
  ~Grille(){delete[] g;};

};


#endif


main.cpp:
#include <iostream>
#include "Personnage.h"
#include "Canard.h"
#include "stdio.h"
#include "Grille.h"
using namespace std;

int main() {
  Personnage * p[3];
  p[0] = new Canard("C","Rambo_Cannard", 100, false, 4, 10, 0, 0, 5, 5);
  p[1] = new Canard("C","mini_Cannard", 100, false, 4, 10, 0, 0, 5, 5);
  p[2] = new Canard("C","mechant_Cannard", 100, false, 4, 10, 0, 0, 5, 5);
 
  cout << "OK" << endl;

 Grille ferme(p);


for(int i=0 ; i<3 ; i++)
{
     delete  p[i];
}
  return 0;
}


Personnage.h:

#include <iostream>
using namespace std;


#ifndef PERSONNAGE_H_
#define PERSONNAGE_H_

class Personnage {
public:
	Personnage();
	Personnage(string Symbole,string Nom);
	
	virtual ~Personnage(){}


	
	Personnage(const Personnage & p){
	_S=p._S;
	_nom=p._nom ;

	}

 Personnage & operator =(const Personnage &p){
 if(this != &p){
	_S=p._S;
	_nom=p._nom ;
	
   return *this;
    }
}


Voila, des idées ?
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
up !
0