Reputation: 1024
I tried to reformulate the question completely. The situation is as follows :
class Departure {
private :
unsigned int supply;
public :
Departure(int);
~Departure();
int getSupply() {
return supply;
};
};
class Route {
private :
Departure* departure;
public :
Departure& getDeparture() {
return *departure;
};
};
class Plan {
private :
std::map<Route*, double> quantities;
public :
void addRoute(Route& rt) {
quantities.insert(std::pair<Route*,double>(&rt,0));
};
void createSolution() {
std::map<Route*, double>::iterator it;
for(it = quantities.begin(); it != quantities.end(); ++it) {
it->second = it->first->getDeparture().getSupply();
};
};
After the routes are added - i have checked, it works OK, i get a segfault which from my trial & error up to now is situated in the it->first->getDeparture().getSupply()
call. Any explanation why?
Upvotes: 1
Views: 2628
Reputation: 25739
I would guess (99% sure) that you are facing a problem of dangling pointers, that is, the pointers to Route and Departure objects are no longer valid when you call createSolution.
The easy fix to that is to not use pointers but instead store the Route and Departure objects as values. There is anyway not much benefit in storing them as pointers, considering that the objects are so small...
Upvotes: 1