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
}
誰が何が間違っているのか知っていますか?