C++ "was not declared in this scope" compile error


Question

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

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.

1
10
3/23/2009 5:41:42 PM

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.

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.


How to fix:

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);

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

14
5/23/2017 10:27:35 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon