Segmentation error in stl map <>

I am not very good at C ++, but I need to test a reputation based system. Below is a snippet of code that segfault gives me when I run it on an ubuntu system. As I wrote in the comments, the two functions "tackleFirstHandInfo ()" and "updateReputation ()" individually execute correctly, but when I call one function from another, it fails. Any help would be greatly appreciated, thanks in advance. code 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 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;
}
+3
source share
4 answers

One of your main problems arises in both functions and is as follows:

if (it == fHandInfo.end()){
    // Some code that doesn't alter 'it'
}
(it->second)->updateRep(behaviour);

it , , it->second undefined. - , it , find insert, ( , ), it .

Edit

:

class RepSys {
private:
    ratingTable repTable; 
    map<int, Rating*> fHandInfo;
    Rating* rating;

typedef ed ratingTable map<int, Rating*>. typedef , .

rating - , , -, . , .

delete rating, . rating, / std::map<int, Rating>, . , rating , .

+10
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);

// , it // . , (it- > second) - UB.

:

 Rating* rating = NULL;
map<int, Rating*>::iterator it = repTable.find(address);
if (it == repTable.end())
{
 rating = new Rating(); 
repTable[address] = rating;
}
else
{
 rating = it->second;
}

- > updateRep ();

+2

segfault, , : address, .

int main() {
    int address;    
    RepSys repsys;
    while (address != 0)
    {
// ...
    }

, , , address. segfault, 0, .

, .

+1

- - , .

IF().

if (it == repTable.end()){
   cout << "Adding New Entry for (repTable) "<< address <<endl;
   rating = new Rating(); 
   repTable[address] = rating;
}

repTable [address] , .

catch . , .

0

Source: https://habr.com/ru/post/1722353/


All Articles