mercredi 12 juillet 2017

Segment fault when using STL sort to sort vector

///////defination of Center//////////////
class Center {
public:
    Center (
        Node centerNode,
        Node presentedNode,
        bool type  //1 = in, 0 = out
) {
centerNode_ = make_shared<Node>(centerNode);
presentedNode_ = make_shared<Node>(presentedNode);
};

~Center(){};

shared_ptr<Node> centerNode_;
shared_ptr<Node> presentedNode_;
bool type_;
};

///////defination of Node//////////////
class Node {
public:
Node(
    int32_t id = 0,
    double x = 0,
    double y = 0,
    bool selected = false,
    bool selectedCandidate = false
);

~Node() {};

int32_t id_;
double x_;
double y_;
bool selected_;
bool selectedCandidate_;
vector<int> neibours_;
};
//////////compare function//////////////

bool compareCenterSCC(const Center* center1, const Center* center2) {
if (center1->centerNode_->x_ == center2->centerNode_->x_ &&
    center1->centerNode_->y_ == center2->centerNode_->y_) {
    return center1->type_ > center2->type_;
}
return false;
}
/////main/////
int main() {
    vector<Center* > candidateCenter;
    Center* aa = new Center(*startQueryNode, *newQuery_, 1);
    candidateCenter.push_back(aa);
    candidateCenter.push_back(bb);////omit several push back operation
    sort(candidateCenter.begin(), candidateCenter.end(), compareCenterSCC);
}

When I run the code, there is a segment fault occurs in the STL sort processing. I think the reason is that stack is full. So I change the node into shared_ptr in Center. But it doesn't help. Does anyone know why? result

Aucun commentaire:

Enregistrer un commentaire