저는 C++에별로 좋지 않지만 평판 기반 시스템을 테스트해야합니다. 우분투 시스템에서 실행할 때 segfault를주는 코드 조각이 아래에 나와 있습니다. 주석에서 두 함수 "tackleFirstHandInfo()"와 "updateReputation()"을 개별적으로 올바르게 실행했지만 다른 함수에서 하나의 함수를 호출하면 충돌이 발생합니다. 어떤 도움이라도 미리 감사드립니다. 코드는 다음과 같습니다 :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;
}
누군가 서식을 수정할 수 있습니까? –
서식을 잘 관리하십시오! – jldupont
이제 읽을 수 있습니다. 비록 사람들이 어쨌든 그것을 알아 냈습니다. –