我有一個嚴重的問題,無法在任何地方找到答案。我希望有人能夠幫助我。將對象存儲在樹中給出了分段錯誤(C++)
首先我嘗試創建與存儲在
信息的數組樹的Node.h文件
class Node {
public:
Node();
void setPerson(Person* _p) {
this->person = _p;
}
Node* getNode(int i) { return nodes[i];}
void insert(Person* _p, Tele* _tele, int i);
private:
Node *nodes[10];
Person* person;
};
Node.cpp
Node::Node() {
for(int i=0;i<10;i++) { nodes[i] = new Node(); }
person = new Person();
}
void Node::insert(Person* _p, Tele _tele, int i) {
std::string t=tele.getString();
if(t.size()==i) {
this->person = _p; // here comes the segmentation fault
} else {
char charNode t.at(i);
int nextNode = charNode - '0';
nodes[nextNode]->insert(_p,_tele,++i);
}
}
Node()的構造函數是做什麼的?特別是,你如何踐踏成員'節點*節點[10]'? – Nim
構造函數啓動新節點和新人對象; – user3219523
我的水晶球認爲你還沒有分配陣列中的所有「節點」。 – molbdnilo