The relevant (I think) lines of my coding are below.
What is meant is that there is a list of Clusters.
One of them, base, will absorb another one (the aborbed).
The aborbed cluster should be erased from the list.
First problem I encountered was that I needed to perform other operations in both base and absorbed clusters, after while cycle closes.
From my searches, I found the = &(*li) stuff. What I understand is that I get a pointer to the address of the element li points to, although I cannot do absorbed = li, because one is an iterator and the other a (simple?) ponter. I’d appreciate some explanation on this.
Now, the bigger problem is that I get a sementation fault in the line c->getPoints(); of the method joinCluster()
What am I doing wrong? What should I do an why?
I’m using g++ (GCC) 4.5.2 in Linux x86_64.
Cluster * base;
Cluster * absorbed;
list<Cluster>::iterator li = clusters.begin();
while ( li != clusters.end() ) {
if (li->getId() == p2) {
absorbed = &(*li);
li = clusters.erase(li);
} else if (li->getId() == p1) {
base = &(*li);
}
++li;
}
base->joinCluster(absorbed);
void Cluster::joinCluster(Cluster * c)
{
set<unsigned int> pts = c->getPoints();
}
set<unsigned int> Cluster::getPoints()
{
return points;
}
class Cluster {
private:
std::set<unsigned int> points;
public:
std::set<unsigned int> getPoints();
};
When you delete then last element in the list,
libecomescluster.end(). Then you increment it again and boom, you’re out of bounds.++lishould go inelseblock.Note also, when you erase
lifrom the container,absorbedholds an invalid adress.