C ++“未在此范围中声明”编译错误 [英] C++ "was not declared in this scope" compile error

查看:586
本文介绍了C ++“未在此范围中声明”编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++的新功能。在下面的程序中,我写的我得到这个错误:

New to C++. In the following program I'm writing I get this error:

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

这是代码:

#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.

推荐答案

错误:

nonrecursivecountcells的定义没有名为grid的参数。您需要将类型AND变量名称传递给函数。你只传递了类型。

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.

请注意,如果您使用名称网格作为参数,该名称与网格的main()声明无关。你也可以使用任何其他名称。

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.

*** 也不能将数组作为值传递。

***Also you can't pass arrays as values.


如何修复

修复此问题的简单方法是将指向数组的指针传递给函数nonrecursivecountcells。

The easy way to fix this is to pass a pointer to an array to the function "nonrecursivecountcells".

int nonrecursivecountcells(color[ROW_SIZE][COL_SIZE], int, int);

更好并输入安全 - >

better and type safe ->

int nonrecursivecountcells(color (&grid)[ROW_SIZE][COL_SIZE], int, int);

关于范围:

在声明的块被终止时,在栈上创建的变量超出范围。块是开头和匹配结束括号内的任何内容。例如,if(){},function(){},while(){},...

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 matching closing brace. For example an 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.

这篇关于C ++“未在此范围中声明”编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆