【发布时间】:2016-04-09 14:17:24
【问题描述】:
代码:
void case1();
void case2();
void case3();
void case4();
void case5();
//Global variables
const int MAXROW = 5;
const int MAXCOL = 5;
int main()
{
........
...
MENU(with do while)
}
void case1()
{
int A[MAXROW][MAXCOL] = { 0 };
for (int r = 0; r < MAXROW; ++r)
for (int c = 0; c < MAXCOL; ++c) {
cout << "\n A[" << r << "][" << c << "]= ";
cin >> A[r][c];
}
}
void case2()
{
...
}
//Function to find average of negatives
void case3()
{
int negNumber = 0;
double average = 0;
for (int r = 0; r < 6; ++r) {
for (int c = 0; c < 6; ++c) {
if...
}
void case4()
{//Function to find product of numbers different from 0
...
}
void case5()
{
...
}
如您所见,输入数组位于case1() 中。我想知道的是如何在所有其他函数中使用这个数组(case2、case3、case4、case5)。
我该怎么做?
如何在菜单中调用它们?例如case2():
case '2': {
case2(......);
目前我的错误列表中充满了类似
的消息'A':未声明的标识符
【问题讨论】:
-
请通过editminimal reproducible example 提出您的问题。
-
你可以通过完全删除来稍微缩短它,但仍然没有那么接近最小。我建议您删除它,花一些时间重新编写(并重建您的示例),并且仅在您准备好取消删除和编辑它时。 (按原样,你只会收集反对票和接近票...)
-
在函数中本地定义的任何数组都不能在其他函数中看到。正如您所问的,您需要在全局范围内定义该数组(不是我认为这是一个好主意)。
标签: c++ arrays parameter-passing pass-by-reference