Je ne suis pas très bon en C++ mais je dois tester un système basé sur la réputation. Un fragment de code est donné ci-dessous ce qui me donne une erreur de segmentation quand je l'exécute sur le système d'ubuntu. Comme je l'ai écrit dans les commentaires, deux fonctions "tackleFirstHandInfo()" et "updateReputation()" s'exécutent correctement mais lorsque j'appelle une fonction de l'autre, elle plante. Toute aide sera grandement appréciée, merci d'avance. le code est ci-dessous:Erreur de segmentation dans la carte stl <>
"ex.h"
#ifndef _ex_h
#define _ex_h
#include "iostream"
#include <map>
#define FADING 0.9
enum Behaviour {FORWARDING, NOTFORWARDING};
class Rating
{
private:
double reputation;
public:
Rating() { reputation = 5.0; }
Rating(double rep) {reputation = rep;}
~Rating() {}
double getRep() { return reputation; }
void updateRep(Behaviour behaviour) {
if (behaviour == FORWARDING)
reputation = reputation + 1;
else
reputation = reputation - 1;
}
};
#endif
"ex.cc"
#include <map>
#include <string>
#include <iostream>
#include "ex.h"
using namespace std;
typedef map<int, Rating*> ratingTable;
class RepSys {
private:
ratingTable repTable;
map<int, Rating*> fHandInfo;
Rating* rating;
public:
RepSys(){}
~RepSys(){}
void tackleFirstHandInfo(int address, Behaviour behaviour)
/* This Function and the function below individually run correctly */
{
map<int, Rating*>::iterator it;
it=fHandInfo.find(address);
if (it == fHandInfo.end()) {
cout << "Adding New Entry for (fHandInfo) "<< address <<endl;
rating = new Rating();
fHandInfo[address] = rating;
}
(it->second)->updateRep(behaviour);
cout<<"First Hand Reputation of "<<address<<"\t is ="<< (it->second)->getRep()<<endl;
updateReputation(address, behaviour); // This causes SegFault !!!!
return;
}
void updateReputation(int address, Behaviour behaviour)
{
map<int, Rating*>::iterator it;
it = repTable.find(address);
if (it == repTable.end()) {
cout << "Adding New Entry for (repTable) "<< address <<endl;
rating = new Rating();
repTable[address] = rating;
}
(it->second)->updateRep(behaviour);
cout<<"Reputation of "<<address<<"\t is ="<< (it->second)->getRep()<<endl;
}
};
int main() {
int address;
RepSys repsys;
while (address != 0)
{
cout << "Address\n";
cin >> address;
repsys.tackleFirstHandInfo(address, FORWARDING);
}
return 0;
}
Quelqu'un pourrait-il corriger le formatage? –
veuillez prendre soin du formatage! – jldupont
Là, c'est lisible maintenant. Même si je vois que les gens l'ont compris de toute façon. –