C++“未在此范围内声明”编译错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/674448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ "was not declared in this scope" compile error
提问by Brian R. Bondy
New to C++. In the following program I'm writing I get this error:
C++ 新手。在我正在编写的以下程序中,我收到此错误:
g++ -o Blob blob.cc
blob.cc: In function 'int nonrecursivecountcells(color (*)[7], int, int)':
blob.cc:41: error: 'grid' was not declared in this scope
Here is the code:
这是代码:
#include <iostream>
enum color {BACKGROUND, ABNORMAL, TEMPORARY};
const int ROW_SIZE = 7;
const int COL_SIZE = 7;
int nonrecursivecountcells(color[ROW_SIZE][COL_SIZE], int, int);
using namespace std;
int main()
{
color grid[ROW_SIZE][COL_SIZE] =
{{BACKGROUND, ABNORMAL, BACKGROUND, ABNORMAL, BACKGROUND, BACKGROUND, BACKGROUND},
{ABNORMAL, ABNORMAL, BACKGROUND, ABNORMAL, BACKGROUND, BACKGROUND, BACKGROUND},
{BACKGROUND, BACKGROUND, ABNORMAL, ABNORMAL, BACKGROUND, BACKGROUND, ABNORMAL},
{BACKGROUND, BACKGROUND, BACKGROUND, ABNORMAL, ABNORMAL, ABNORMAL, BACKGROUND},
{BACKGROUND, BACKGROUND, BACKGROUND, ABNORMAL, BACKGROUND, BACKGROUND, BACKGROUND},
{BACKGROUND, BACKGROUND, BACKGROUND, BACKGROUND, BACKGROUND, ABNORMAL, BACKGROUND},
{BACKGROUND, ABNORMAL, ABNORMAL, BACKGROUND, BACKGROUND, BACKGROUND, ABNORMAL}};
cout << "Enter row number" << endl;
int row;
cin >> row;
cout << "Enter column number" << endl;
int column;
cin >> column;
int number = nonrecursivecountcells(grid, row, column);
cout << "Number off cells in the blob that contains grid[" << row << "][" << column << "] are: " << number << endl;
return 0;
}
int nonrecursivecountcells(color[ROW_SIZE][COL_SIZE], int row, int column)
{
if (row < 0 || row >= ROW_SIZE || column < 0 || column >= COL_SIZE)
{
return 0;
}
else if (grid[row][column] != ABNORMAL)
{
return 0;
}
else
{
grid[row][column] = TEMPORARY;
return 1
+ nonrecursivecountcells(grid, row - 1, column - 1) + nonrecursivecountcells(grid, row - 1, column)
+ nonrecursivecountcells(grid, row - 1, column + 1) + nonrecursivecountcells(grid, row, column + 1)
+ nonrecursivecountcells(grid, row + 1, column + 1) + nonrecursivecountcells(grid, row + 1, column)
+ nonrecursivecountcells(grid, row + 1, column - 1) + nonrecursivecountcells(grid, row, column - 1);
}
}
Can anyone help me out here? Thanks.
有人可以帮我从这里出去吗?谢谢。
回答by Brian R. Bondy
What's wrong:
怎么了:
The definition of "nonrecursivecountcells" has no parameter named grid. You need to pass the type AND variable name to the function. You only passed the type.
“nonrecursivecountcells”的定义没有名为 grid 的参数。您需要将类型和变量名称传递给函数。你只传递了类型。
Note if you use the name grid for the parameter, that name has nothing to do with your main() declaration of grid. You could have used any other name as well.
请注意,如果您使用名称 grid 作为参数,则该名称与您的 main() grid 声明无关。您也可以使用任何其他名称。
***
Also you can't pass arrays as values.
How to fix:
怎么修:
The easy way to fix this is to pass a pointer to an array to the function "nonrecursivecountcells".
解决此问题的简单方法是将指向数组的指针传递给函数“nonrecursivecountcells”。
int nonrecursivecountcells(color[ROW_SIZE][COL_SIZE], int, int);
better and type safe ->
更好,输入安全 ->
int nonrecursivecountcells(color (&grid)[ROW_SIZE][COL_SIZE], int, int);
About scope:
关于范围:
A variable created on the stack comes out of scope when the block it is declared in is terminated. A block is anything within an opening and matchingclosing brace. For example an if() { }, function() { }, while() {}, ...
当声明它的块终止时,在堆栈上创建的变量超出范围。块是在左括号和匹配右括号内的任何东西。例如 if() { }, function() { }, while() {}, ...
Note I said variable and not data. For example you can allocate memory on the heap and that data will still remain valid even outside of the scope. But the variable that originally pointed to it would still come out of scope.
注意我说的是变量而不是数据。例如,您可以在堆上分配内存,即使超出范围,该数据仍将保持有效。但是最初指向它的变量仍然会超出范围。
回答by Evan Teran
grid is not a global, it is local to the main function. Change this:
grid 不是全局的,它是主函数的局部。改变这个:
int nonrecursivecountcells(color[ROW_SIZE][COL_SIZE], int row, int column)
to this:
对此:
int nonrecursivecountcells(color grid[ROW_SIZE][COL_SIZE], int row, int column)
Basically you forgot to give that first param a name, grid will do since it matches your code.
基本上你忘了给第一个参数一个名字,网格会做,因为它匹配你的代码。
回答by JeffH
The first argument to nonrecursivecountcells() doesn't have a variable name. You try to reference it as grid in the function body, so you probably want to call it grid.
nonrecursivecountcells() 的第一个参数没有变量名。您尝试在函数体中将其引用为 grid,因此您可能希望将其称为 grid。
回答by rmeador
As the compiler says, grid
was not declared in the scope of your function :) "Scope" basically means a set of curly braces. Every variable is limited to the scope in which it is declared (it cannot be accessed outside that scope). In your case, you're declaring the grid
variable in your main()
function and trying to use it in nonrecursivecountcells()
. You seem to be passing it as the argument colors
however, so I suggest you just rename your uses of grid
in nonrecursivecountcells()
to colors
. I think there may be something wrong with trying to pass the array that way, too, so you should probably investigate passing it as a pointer (unless someone else says something to the contrary).
正如编译器所说,grid
未在您的函数范围内声明:)“范围”基本上意味着一组花括号。每个变量都被限制在声明它的范围内(不能在该范围外访问)。在您的情况下,您grid
在main()
函数中声明变量并尝试在nonrecursivecountcells()
. colors
但是,您似乎将其作为参数传递,因此我建议您将grid
in的使用重命名nonrecursivecountcells()
为colors
。我认为尝试以这种方式传递数组也可能有问题,因此您可能应该调查将其作为指针传递(除非其他人说相反的话)。
回答by bayda
fix function declaration on
修复函数声明
int nonrecursivecountcells(color grid[ROW_SIZE][COL_SIZE], int row, int column)
回答by Pablo Santa Cruz
gridis not present on nonrecursivecountcells's scope.
网格不存在于 nonrecursivecountcells 的范围内。
Either make grida global array, or pass it as a parameter to the function.
要么使grid成为全局数组,要么将其作为参数传递给函数。