Pb perte initialisation variable C++

smagf Messages postés 4 Date d'inscription samedi 27 novembre 2004 Statut Membre Dernière intervention 24 janvier 2005 - 24 janv. 2005 à 20:42
smagf Messages postés 4 Date d'inscription samedi 27 novembre 2004 Statut Membre Dernière intervention 24 janvier 2005 - 24 janv. 2005 à 20:44
Bonjour, alors voici mon pb :



pDecrypter = new Computer(duplicates)
un nouvel objet de type Computer est créé
Computer::Computer(bool dupes):
Decrypter(dupes), total(0), mySmartString(0)

{
}
cela appelle constr Decrypter:ecrypter(bool dupes):
round(0),
duplicates (dupes)
{
}

et les variables membres mySmartString et total sont initialisées à 0

Ensuite il ya pDecrypter->Play();
A ce moment là pdecrypter affiche seulement les variables protected(de la classe DECRYPTER)
cependant il va bien dans Computer:lay
if (!mySmartString)
mySmartString = new SmartString(duplicates);
et là je me rends compte avec stupéfaction que mySmartString est rempli avec n'importe quoi!!! et ça me plante tout!! segmentation defaultPouvez vous m'aider et me dire ce qui s'est passé.
La variable total contient également n'importe quoi
En fait juste avant la ligne pDecrypter->Play (qui est dans game.cpp), pDecrypter est considéré alors comme un objet de type DECRYPTER avec la definition de DECRYPTER ( c normal qu'il ne soit pas considéré comme computer?)et quand je me debranche dans COMPUTER :: PLAY, alors le pointeur this est considéré comme un objet COMPUTER mais là "smartstring" ET "total" sont initialisés avec n'importe quoi.


J'ai donc appremment un pb de relation entre les classes Computer et Decrypter mais je ne vois pas c equi ne marche pas
Les classes human et computer sont des classes dérivées de Decrypter.


Voici le code en totalité:


1a) la classe Game
[cpp]
#ifndef GAME_HPP
#define GAME_HPP
#include "defvals.hpp"
#include "Gues1205.hpp"
#include "SmartString1201.hpp"


class Guess;
class Decrypter;
class Game
{
public:
Game();
~Game() {}


void Play();


static int howManyLetters;
static int howManyPositions;



private:
void DisplayTime(int secs);
bool VerifyComputerChoices();
bool duplicates;



Decrypter * pDecrypter;
};


#endif


/cpp
1b) l'implémentation de la classe Game
[cpp]
#include "game1103.hpp"
#include "defvals.hpp"
#include "decrypter1101.hpp"
#include "human1105.hpp"
#include "computer1109.hpp"
#include "SmartString1201.hpp"


#include
#include


using namespace std;


char * Accent(const char * mess) {
static char retour [80];
CharToOem (mess,retour);
return retour;
}





int Game::howManyLetters = 0;
int Game::howManyPositions = 0 ;


Game::Game()
{
for( ;; )


{
// Récupération du choix de l'utilisateur
// pour le nombre de lettres possible


while (howManyLetters < minLetters
|| howManyLetters > maxLetters)
{
cout Display();
}
}



/cpp



5a) la classe SmartSrting
[cpp]
#ifndef SMARTSTRING_HPP
#define SMARTSTRING_HPP


#include "defvals.hpp"
#include "smartchr1203.hpp"


class Guess;
class SmartString
{
public:
SmartString(bool dupes);
virtual ~SmartString();


bool CanEliminateCharacters(const Guess& theGuess);
bool GetNext();
vector GetString();
bool RemoveCurrentCharacters();
bool RemoveCurrentCharactersInEveryPosition();


private:
void ForceCharacters(const Guess& theGuess);
int CountForcedInGuess(const Guess& theGuess);
int CountUniqueLettersInGuess(const Guess & theGuess);
bool In(vector vec, char target) const;


vector deadCharacters;
bool duplicates;
vector forcedCharacters;
vector myString;
};


#endif


/cpp
5b) l'implémentation de la classe SmartString
[cpp]
#include "Gues1205.hpp"
#include "game1103.hpp"
#include "SmartString1201.hpp"


#include


using namespace std;





SmartString::SmartString(bool dupes):
duplicates(dupes)


{
for (int i = 0; i < Game::howManyPositions; i++)
{
int j;
if (duplicates)
j = 0 ;
else
j = i;
SmartChar theChar(j);
myString.push_back(theChar);
}
}


SmartString :: ~SmartString()
{
}


vector SmartString::GetString()
{


vector outString;


for (vector::iterator it = myString.begin();
it != myString.end();
it ++)
{
char theChar = it->GetChar();
outString.push_back(theChar);
}


return outString;
}


bool SmartString::GetNext()
{


vector outString;
vector::reverse_iterator rit;
rit = myString.rbegin();


bool rollover = rit->Increment();
while(rollover)
{
rit ++;
if (rit == myString.rend())
return false;
else
{
rollover = rit->Increment();
}
}
return true;
}


// enlève le character se trouvant actuellment
// à une certaine position


bool SmartString::RemoveCurrentCharacters()
{


char theChar;
bool anyDeleted = false;


for (vector::iterator it = myString.begin();
it != myString.end();
it ++)
{
theChar = it->GetChar();


if (! In(forcedCharacters, theChar) )
{
theChar = it->RemoveCurrent();
// "deadcharacters" ( caractères morts) évite la prise
// en compte de caractères déjà exclus
if ( ! In(deadCharacters, theChar) )
{
deadCharacters.push_back(theChar);
cout = myCharacters.size() )
{
myChar = 0;
return true;
}
return false;
}


char SmartChar::RemoveCurrent()
{
char theChar = ' ';
if (myCharacters.size() > 1)
{
theChar = GetChar();
myCharacters.erase(myCharacters.begin() +myChar);
while ( myChar >= myCharacters.size() )
myChar--;
}
return theChar;
}


bool SmartChar::Remove (char theChar)
{
if (myCharacters.size() > 1)


{
vector::iterator where =
find ( myCharacters.begin(), myCharacters.end(), theChar);
if ( where != myCharacters.end())
myCharacters.erase(where);
return true;
}
return false;
}
/cpp



7a) la classe Guess
[cpp]
#ifndef GUESS_HPP
#define GUESS_HPP
#include
#include "defvals.hpp"
using namespace std ;


class Guess
{
public:
Guess();
Guess(vector guess , int howManyRight, int howManyInPosition);
~Guess(){};
pair GetScore() const {return score;}
vector GetString() const {return myString;}
void Display() const;
private:
vector myString ;
pair score;
};


#endif


/cpp
7b) l'implémentation de la classe Guess
[cpp]
#include "Gues1205.hpp"


Guess::Guess(vectorguess, int howManyRight, int howManyInPosition):
myString(guess),
score(howManyRight, howManyInPosition)
{
}



void Guess::Display() const


{


copy ( myString.begin(),
myString.end(),
ostream_iterator(cout, " ") );
cout

1 réponse

smagf Messages postés 4 Date d'inscription samedi 27 novembre 2004 Statut Membre Dernière intervention 24 janvier 2005
24 janv. 2005 à 20:44
Bonjour, alors voici mon pb :



pDecrypter = new Computer(duplicates)
un nouvel objet de type Computer est créé
Computer::Computer(bool dupes):
Decrypter(dupes), total(0), mySmartString(0)

{
}
cela appelle constr Decrypter:ecrypter(bool dupes):
round(0),
duplicates (dupes)
{
}

et les variables membres mySmartString et total sont initialisées à 0

Ensuite il ya pDecrypter->Play();
A ce moment là pdecrypter affiche seulement les variables protected(de la classe DECRYPTER)
cependant il va bien dans Computer:lay
if (!mySmartString)
mySmartString = new SmartString(duplicates);
et là je me rends compte avec stupéfaction que mySmartString est rempli avec n'importe quoi!!! et ça me plante tout!! segmentation defaultPouvez vous m'aider et me dire ce qui s'est passé.
La variable total contient également n'importe quoi
En fait juste avant la ligne pDecrypter->Play (qui est dans game.cpp), pDecrypter est considéré alors comme un objet de type DECRYPTER avec la definition de DECRYPTER ( c normal qu'il ne soit pas considéré comme computer?)et quand je me debranche dans COMPUTER :: PLAY, alors le pointeur this est considéré comme un objet COMPUTER mais là "smartstring" ET "total" sont initialisés avec n'importe quoi.


J'ai donc appremment un pb de relation entre les classes Computer et Decrypter mais je ne vois pas c equi ne marche pas
Les classes human et computer sont des classes dérivées de Decrypter.


Voici le code en totalité:


1a) la classe Game

#ifndef GAME_HPP
#define GAME_HPP
#include "defvals.hpp"
#include "Gues1205.hpp"
#include "SmartString1201.hpp"


class Guess;
class Decrypter;
class Game
{
public:
Game();
~Game() {}


void Play();


static int howManyLetters;
static int howManyPositions;



private:
void DisplayTime(int secs);
bool VerifyComputerChoices();
bool duplicates;



Decrypter * pDecrypter;
};


#endif



1b) l'implémentation de la classe Game

#include "game1103.hpp"
#include "defvals.hpp"
#include "decrypter1101.hpp"
#include "human1105.hpp"
#include "computer1109.hpp"
#include "SmartString1201.hpp"


#include
#include


using namespace std;


char * Accent(const char * mess) {
static char retour [80];
CharToOem (mess,retour);
return retour;
}





int Game::howManyLetters = 0;
int Game::howManyPositions = 0 ;


Game::Game()
{
for( ;; )


{
// Récupération du choix de l'utilisateur
// pour le nombre de lettres possible


while (howManyLetters < minLetters
|| howManyLetters > maxLetters)
{
cout Display();
}
}







5a) la classe SmartSrting

#ifndef SMARTSTRING_HPP
#define SMARTSTRING_HPP


#include "defvals.hpp"
#include "smartchr1203.hpp"


class Guess;
class SmartString
{
public:
SmartString(bool dupes);
virtual ~SmartString();


bool CanEliminateCharacters(const Guess& theGuess);
bool GetNext();
vector GetString();
bool RemoveCurrentCharacters();
bool RemoveCurrentCharactersInEveryPosition();


private:
void ForceCharacters(const Guess& theGuess);
int CountForcedInGuess(const Guess& theGuess);
int CountUniqueLettersInGuess(const Guess & theGuess);
bool In(vector vec, char target) const;


vector deadCharacters;
bool duplicates;
vector forcedCharacters;
vector myString;
};


#endif



5b) l'implémentation de la classe SmartString

#include "Gues1205.hpp"
#include "game1103.hpp"
#include "SmartString1201.hpp"


#include


using namespace std;





SmartString::SmartString(bool dupes):
duplicates(dupes)


{
for (int i = 0; i < Game::howManyPositions; i++)
{
int j;
if (duplicates)
j = 0 ;
else
j = i;
SmartChar theChar(j);
myString.push_back(theChar);
}
}


SmartString :: ~SmartString()
{
}


vector SmartString::GetString()
{


vector outString;


for (vector::iterator it = myString.begin();
it != myString.end();
it ++)
{
char theChar = it->GetChar();
outString.push_back(theChar);
}


return outString;
}


bool SmartString::GetNext()
{


vector outString;
vector::reverse_iterator rit;
rit = myString.rbegin();


bool rollover = rit->Increment();
while(rollover)
{
rit ++;
if (rit == myString.rend())
return false;
else
{
rollover = rit->Increment();
}
}
return true;
}


// enlève le character se trouvant actuellment
// à une certaine position


bool SmartString::RemoveCurrentCharacters()
{


char theChar;
bool anyDeleted = false;


for (vector::iterator it = myString.begin();
it != myString.end();
it ++)
{
theChar = it->GetChar();


if (! In(forcedCharacters, theChar) )
{
theChar = it->RemoveCurrent();
// "deadcharacters" ( caractères morts) évite la prise
// en compte de caractères déjà exclus
if ( ! In(deadCharacters, theChar) )
{
deadCharacters.push_back(theChar);
cout = myCharacters.size() )
{
myChar = 0;
return true;
}
return false;
}


char SmartChar::RemoveCurrent()
{
char theChar = ' ';
if (myCharacters.size() > 1)
{
theChar = GetChar();
myCharacters.erase(myCharacters.begin() +myChar);
while ( myChar >= myCharacters.size() )
myChar--;
}
return theChar;
}


bool SmartChar::Remove (char theChar)
{
if (myCharacters.size() > 1)


{
vector::iterator where =
find ( myCharacters.begin(), myCharacters.end(), theChar);
if ( where != myCharacters.end())
myCharacters.erase(where);
return true;
}
return false;
}




7a) la classe Guess

#ifndef GUESS_HPP
#define GUESS_HPP
#include
#include "defvals.hpp"
using namespace std ;


class Guess
{
public:
Guess();
Guess(vector guess , int howManyRight, int howManyInPosition);
~Guess(){};
pair GetScore() const {return score;}
vector GetString() const {return myString;}
void Display() const;
private:
vector myString ;
pair score;
};


#endif



7b) l'implémentation de la classe Guess

#include "Gues1205.hpp"


Guess::Guess(vectorguess, int howManyRight, int howManyInPosition):
myString(guess),
score(howManyRight, howManyInPosition)
{
}



void Guess::Display() const


{


copy ( myString.begin(),
myString.end(),
ostream_iterator(cout, " ") );
cout
0
Rejoignez-nous