所以這是我的代碼:爲什麼這個循環沒有檢測到空指針?
while(node) {
cout<<node->getDiameter();
node=node->stepAbove();
}
這些都是方法:
Node* Node::stepAbove() {
return this->above;
}
int Node::getDiameter() {
return this->r;
}
但while循環導致訪問衝突,因爲迴路沒有檢測到空指針。調試時,它指向一個沒有定義的地址「0xcccccccc」......任何問題的建議?
編輯:忘了後我的構造函數是:
Node(int x=0) {
this->above=nullptr;
this->r=x;
}
聽起來你實際上沒有空指針... –
它不是null,它是未初始化的。很可能在'this-> above'行。 – azz
這是令我困惑的事情。爲什麼它沒有指向null,因爲它從未初始化? – NitroNbg