I am getting a segmentation fault in my program and gdb tells me it is in this function on the line of
parent->getChildren().push_back(temp);
in
void Tree::add(Position& value, Node*& parent) {
Node* temp = new Node(value, parent);
parent->getChildren().push_back(temp);
}
I have added cout statements before that line and everything seems to be valid when the function is called. But I don’t think my vector can be invalid? The vector declaration is here –
std::vector<Node*> children;
with getChildren() just returning std::vector&. Any help is appreciated.
Node constructor:
Tree::Node::Node(Position& v, Node*& p)
: value(v), parent(p), gvalue(0), hvalue(0), fvalue(0) {}
That can’t be an “element problem” because you just
push_back(Node*). That can’t fail.So I see 2 possible “vector problems”:
parent->becauseparentis not allocated.getChildren().because it returns a reference to non-existing vector.Try to check both of them.