对象数组c ++指针

时间:2014-11-07 04:48:13

标签: c++ arrays pointers

这是我的quadTree.h文件

class quadTree{
public :
Rectangle bounds;
quadTree *nodes[4];
int level;
char collision;

void split();
bool containsObstacle( vector<Rectangle> obstacles);

quadTree(int level, Rectangle bounds);
};

这是我的quadTree.cpp文件:

void quadTree:: split(){
int childWidth  = (int)( bounds.getWidth() / 2);
int childHeight = (int)(bounds.getHeight() / 2);

nodes[0] = new quadTree(level + 1 , Rectangle(bounds.getX()+childWidth ,
                                                            bounds.getY() , childWidth , childHeight));

nodes[1] = new quadTree(level + 1 , Rectangle(bounds.getX() , bounds.getY()+childHeight ,
                                                            childWidth , childHeight));

nodes[2] = new quadTree(level + 1 , Rectangle(bounds.getX()+childWidth , bounds.getY()+childHeight ,
                                                         childWidth , childHeight));

nodes[3] = new quadTree(level + 1, Rectangle(bounds.getX() , bounds.getY() , childWidth, childHeight));
}

quadTree::quadTree(int level , Rectangle bounds){
   this -> level = level;
   this -> bounds = bounds;
}

我想要完成的是:

quadTree test (0,Rectangle(0,0,700,700));
test.split();
for (int i = 0; i<4; i++) {
cout<<test.nodes[i]->bounds.getX()<<" "<<test.nodes[i]->bounds.getY();
}

但由于某种原因Xcode,当我尝试运行时,一直给我11db错误。我的问题是我是否正确地将值存储到数组中?我对c ++很新,这是我第一次使用指针。 感谢

0 个答案:

没有答案