I am not sure what is wrong with how I am initializing the variables count
and invFlag
. Are you not allowed to initialize variables inside of an if statement? In case you are wondering this is a portion of my code to check if the maze I solved recursively is correct (i.e. checking if there is a path given by '.' that takes me from the start 'S' to the end 'E'). The maze is being solved correctly in the function solveMaze but this function is simply to check and see if it was solved correctly. I use the variable invFlag to make sure for example that I don't go down and then the next call up because that would be pointless). At the beginning (when I am at the start 'S', I want to initialize this invFlag to a junk value as I did). This is a portion of the code.
我不确定我如何初始化变量count和invFlag有什么问题。您是否不允许在if语句中初始化变量?如果你想知道这是我的代码的一部分,以检查我递归解决的迷宫是否正确(即检查是否有'。'给出的路径,从开始'S'到结束'E' )。迷宫正在函数solveMaze中正确解决,但这个函数只是检查并查看它是否正确解决。我使用变量invFlag来确保例如我不下去然后下一次调用因为那将是毫无意义的。在开始时(当我在开始'S'时,我想像我一样将这个invFlag初始化为垃圾值)。这是代码的一部分。
I am receiving this error:
我收到此错误:
maze.c: In function ‘checkMaze’:
maze.c:151: error: ‘count’ undeclared (first use in this function)
maze.c:151: error: (Each undeclared identifier is reported only once
maze.c:151: error: for each function it appears in.)
maze.c:152: error: ‘invFlag’ undeclared (first use in this function)
make: *** [maze.o] Error 1
A portion of my code:
我的一部分代码:
int checkMaze(char ** maze, int width, int height, int x, int y)
if(maze[y][x] == 'S')
{
int invFlag, count;
invFlag = 4; // junk value
count = 0;
}
if(count <=15)
printf("y: %d x: %d invFlag: %d \n", y, x, invFlag);
count = count+1;
if (maze[y][x] == 'E')
return 1;
/* recursive calls (order: right, up, left, down) */
// if right of current position is '.' move there
if ( ( (maze[y][x+1] == '.') || (maze[y][x+1] == 'E') ) && (invFlag != 2 ) )
{
invFlag = 0; // right move will be performed
checkMaze(maze, width, height, x + 1, y);
}
// if up of current position is '.' move there
else if ( (maze[y-1][x] == '.' || maze[y-1][x] == 'E') && ( invFlag != 3) )
{
invFlag = 1; // up move will be performed
checkMaze(maze, width, height, x, y - 1);
}
// if left of current position is '.' move there
else if ( (maze[y][x-1] == '.' || maze[y][x-1] == 'E') && (invFlag != 0 ))
{
invFlag = 2; // left move will be performed
checkMaze(maze, width, height, x - 1, y);
}
// if down of current position is '.' move there
else if ( (maze[y+1][x] == '.' || maze[y+1][x] == 'E') && (invFlag != 1 ))
{
invFlag = 3; // down move will be performed
checkMaze(maze, width, height, x, y + 1);
}
return 0; // maze solution was not correct
}
2 个解决方案
#1
4
The visibility of the variable count is just within the if()
block
变量计数的可见性就在if()块中
if()
{
//count is visible within if
}
// count is unknown here
Since count is not known outside of the if() block compiler is reporting error this can be fixed by moving the variable declaration outside of the if() block like
由于if()块编译器报告错误之外不知道count,因此可以通过将变量声明移到if()块之外来修复
int count;
if()
{
// count is visible
}
// count is visible
#2
1
The initialization is not the problem, you are declaring them inside the if
block, so they are not declared at the function scope, hence the error.
初始化不是问题,你在if块中声明它们,因此它们不在函数范围内声明,因此错误。
Just move this
移动它吧
int invFlag, count;
outside of the if
block to the function scope, and then it will compile, whether it will work is another thing, it's a bad idea to leave the variable uninitialized unless you are certain that there will not be any access that precedes initialization.
在if块之外的函数范围,然后它将编译,是否它将工作是另一回事,将变量保持未初始化是一个坏主意,除非你确定在初始化之前不会有任何访问。
It seems that you are working with gcc
which would warn about that if you invoke it with -Wall
.
看来你正在使用gcc,如果你用-Wall调用它,就会发出警告。
If there is another declaration of invFlag
then the invFlag
outside the if
block is not the invFlag
that you declared in the if
block, in that case just remove the declaration, and use -Wshadow
to make gcc warn about that.
如果有另一个invFlag声明,那么if块之外的invFlag不是你在if块中声明的invFlag,在这种情况下只需删除声明,并使用-Wshadow让gcc警告它。
#1
4
The visibility of the variable count is just within the if()
block
变量计数的可见性就在if()块中
if()
{
//count is visible within if
}
// count is unknown here
Since count is not known outside of the if() block compiler is reporting error this can be fixed by moving the variable declaration outside of the if() block like
由于if()块编译器报告错误之外不知道count,因此可以通过将变量声明移到if()块之外来修复
int count;
if()
{
// count is visible
}
// count is visible
#2
1
The initialization is not the problem, you are declaring them inside the if
block, so they are not declared at the function scope, hence the error.
初始化不是问题,你在if块中声明它们,因此它们不在函数范围内声明,因此错误。
Just move this
移动它吧
int invFlag, count;
outside of the if
block to the function scope, and then it will compile, whether it will work is another thing, it's a bad idea to leave the variable uninitialized unless you are certain that there will not be any access that precedes initialization.
在if块之外的函数范围,然后它将编译,是否它将工作是另一回事,将变量保持未初始化是一个坏主意,除非你确定在初始化之前不会有任何访问。
It seems that you are working with gcc
which would warn about that if you invoke it with -Wall
.
看来你正在使用gcc,如果你用-Wall调用它,就会发出警告。
If there is another declaration of invFlag
then the invFlag
outside the if
block is not the invFlag
that you declared in the if
block, in that case just remove the declaration, and use -Wshadow
to make gcc warn about that.
如果有另一个invFlag声明,那么if块之外的invFlag不是你在if块中声明的invFlag,在这种情况下只需删除声明,并使用-Wshadow让gcc警告它。