Hi All,
I am not very good at c++ but I have to test a reputation based system. A code fragment is given below which gives me segfault when i run it on ubuntu system. As i wrote in comments two functions "tackleFirstHandInfo()" and "updateReputation()" individually run correctly but when i call one function from the other it crashes. Any help will be greatly appreciated, thanks in advance. the code is below:
"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 Funtion 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;
}
Kind Regards Sohail