我遇到了一个非常奇怪的错误,在我离开for
范围后,即使在类头中声明了包含对象的数组,我也无法访问循环期间指针指向的任何内容。
这是代码的基础:
Class CTile{ /*Code*/ };
Class CMap
{
public:
CTile** tiles;
CMap();
}
CMap::CMap()
{
int lines = 10;
int cols = 10;
tiles = new CTile*[lines];
for(int i = 0 ; i (lower than) lines;++)
{
this->tiles[i] = new CTile[cols];
}
for(int curLine = 0; curLine (lower than) lines ; curLine++)
for(int curCol = 0; curCol (lower than) cols; curCol++)
{
CTile me = this->tiles[curLine][curCol];
me.setType(1);
//do whatever I need, and inside the loop everything works.
}
int a = this->tiles[2][2].getType(); // a gets a really weird number
this->tiles[2][2].setType(10); // crashes the program
}
有谁知道可能出了什么问题?