我搜索了這個網站和谷歌,並沒有真正發現任何解決我的問題。我正在嘗試編寫一個遊戲,並且此遊戲包含玩家可以穿越的地形圖塊地圖。我想將瓷磚存儲在10x10陣列中,但我在初始化陣列時遇到問題。使用二維數組構造C++
我可以初始化數組的第一維,但是錯誤在初始化第一個for循環內的第二維。
這裏是我的代碼如下所示:
//tile on the "map"
struct tile
{
char type;
bool isWall;
};
void initializeMap(tile * map)
{
int index1, index2;
for(index1 = 0; index1 < 10; index1++)
{
map[index1] = new tile[10];
for(index2 = 0; index2 < 10; index2++)
{
}
}
}
int main()
{
tile * tileMap = new tile[10];
initializeMap(tileMap);
return 0;
}
我收到此錯誤:
C:\Users\----\Desktop\TextGame.cpp||In function 'void initializeMap(tile*)':|
C:\Users\----\Desktop\TextGame.cpp|39|error: no match for 'operator=' in '*(map + ((unsigned int)(((unsigned int)index1) * 2u))) = (tile*)operator new [](20u)'|
C:\Users\----\Desktop\TextGame.cpp|9|note: candidates are: tile& tile::operator=(const tile&)|
||=== Build finished: 1 errors, 0 warnings ===|
自從你將'tile *'打包到map中後,它不需要是'tile **'嗎? – zero298
@ zero298很確定這個問題就在那裏,清理一下並作爲回答發佈 –
@DanF現在發佈 – zero298