tags:

views:

14016

answers:

6

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.

+7  A: 

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.

Brian R. Bondy
i caught you run into the pet peeve: http://stackoverflow.com/questions/423823/whats-your-favorite-programmer-ignorance-pet-peeve/484900#484900 :p
Johannes Schaub - litb
the ** and [x][y] syntax aren't interchangeable, you can't convert color grid[x][y] into a color **.
Greg Rogers
@Greg Rogers: You're right, I fixed.
Brian R. Bondy
It isn't as simple as casting, you need to make a secondary array of pointers, where each element points to the beginning of each row of the original 2-d array, and then pass that new array as the parameter.
Greg Rogers
@Greg Rogers: Ya I removed it. Thanks.
Brian R. Bondy
i think this answer is appropriate: http://stackoverflow.com/questions/274865/pointer-question-in-c/274943#274943 . Anyway, i wouldn't have commented on your answer if you in principle could cast and it would work. but here it wouldn't :)
Johannes Schaub - litb
@litb: first comment, agree thanks.
Brian R. Bondy
A: 

grid is not present on nonrecursivecountcells's scope.

Either make grid a global array, or pass it as a parameter to the function.

Pablo Santa Cruz
+4  A: 

grid is not a global, it is local to the main function. Change this:

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.

Evan Teran
The syntax you gave is actually seen as type: color [][COL_SIZE]. Please see my answer to see how to preserve the type as color[ROW_SIZE][COL_SIZE].
Brian R. Bondy
The problem with that is you could accidentally pass in a value that has size [3][COL_SIZE] with that way of declaration.
Brian R. Bondy
A: 

fix function declaration on

int nonrecursivecountcells(color grid[ROW_SIZE][COL_SIZE], int row, int column)
bb
+1  A: 

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.

JeffH
+1  A: 

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

rmeador
+1 for addressing what scope means.However, I think you're wrong about colors. The argument is missing a name. "color" is the data type.
JeffH
@rmeador: yes you are correct about passing arrays as values not being possible.
Brian R. Bondy
yeah, I messed up about the colors thing :) Didn't read the original code closely enough. I'm going to leave it, since to remove it from my post would make the text nonsensical.
rmeador