每次我在Visual Studio 2015中运行此代码时,它都会显示错误标识符" treasureLocation"未定义......我做错了什么?
int main()
{
int gridSize [2];
int gridX = 0;
int gridY = 0;
int treasureLoaction[2];
int end;
std :: cout << "what size grid would you like to play on?" << std :: endl;
std :: cin >> gridSize [1];
std :: cin >> gridSize [2];
treasureLocation[1] = rand() % gridSize[1] + 1;
treasureLocation[2] = rand() % gridSize[2] + 1;
while (gridY < gridSize[2]) {
gridY++;
while (gridX < gridSize[1]) {
std::cout << "* ";
gridX++;
}
std::cout << "" << std::endl;
gridX = 0;
};
std::cout << treasureLoaction;
std::cin >> end;
return 0;
}
答案 0 :(得分:2)
有一个错字
int treasureLoaction[2];
^^^^^^^^
考虑到如果一个数组有n
个元素,那么索引的有效范围是[0, n-1]
这句话
std::cout << treasureLoaction;
不会像您想象的那样输出数组的元素。
您可以在循环中执行此操作,例如
for ( int x : treasureLocation ) std::cout << x << ' ';
std::cout << std::endl;
答案 1 :(得分:1)
尝试遍历数组以打印其内容,如
for (int i = 0; i < your array length; i++) cout << array[i];
除非您尝试打印地址,并且看起来您在treasureLoaction中有拼写错误。