这可能是一个很长的镜头,但我需要帮助弄清楚为什么返回语句在它不应该发送空指针时。我的代码是一个可以遍历的二维链表。如果向下移动然后再向上移动会崩溃。 我的主类将调用下一个节点
if((y-dist)>0){ //if not moved out of bounds
for(int c = 0; c < dist; c++) {//for each step
node.draw(); //mark current nod as visited
if(ptr->getup() != NULL){ //another out of bounds check
ptr = ptr->getup(); //gets the node above it, but returns null
}
}
}
我的节点类
#ifndef GRIDNODE_H
#define GRIDNODE_H
#include <cstddef>
#include "gridnode.h"
#include "grid.h"
#include "basenode.h"
class gridnode : public basenode{
public:
gridnode *l, *r, *u, *d;
gridnode(){
l = NULL;
r = NULL;
u = NULL;
d = NULL;
}
void draw(){mark = pen;}
}
#endinf
和由四联链表制作的50x50的网格
#include <iostream>
#include "grid.h"
#include "gridnode.h"
#include "basenode.h"
grid::grid(){
gridnode *r1, *r2, *p;
r1 = new gridnode;
head = r1;
for (int i = 2; i <= 50; i++ ){
r2 = new gridnode;
r2->l = r1;
r1->r = r2;
r1 = r2;
}
r1 = head;
for (int i = 2; i <= 50; i++){
r2 = new gridnode;
r2->u = r1;
r1->d = r2;
r1 = r2;
for (int j = 2; j <= 50; j++){
p = new gridnode;
p->l = r2;
r2->r = p;
p->u = r2->u->r;
p->u->d = p;
r2 = p;
}
}
sethead(head);
}