My problem is the array Field which is considered by the compiler as undeclared. I declared the array in main and i'm trying to access it through the following .cpp file.


animals.cpp :

#include <cstdlib>
#include "organisms.h"
#include "animals.h" // class's header file

// class constructor
Animals::Animals()
{

}
void Animals::checkMovement()
{
 int gi, gj;
 organisms* temp;                                                           //<--------The problem is here
 do{
  gi = (posi-1)+rand()%(posi+1);
  gj = (posj-1)+rand()%(posj+1);
  if(Field[gi][gj] == NULL)                                               //And here
  {
   temp = Field[posi][posj];
   Field[posi][posj] = Field[gi][gj];
   Field[gi][gj] = temp;
   break;
  }
 }while(Field[gi][gj] != NULL);
 posi = gi;
 posj = gj;
 N++;
}

organisms is the base class and animals is the derived class. How can I fix this? Thank you in advance.

Edited 6 Years Ago by SpyrosMet: n/a

You need to pass the array as an argument to the function...when you call the function from main, give it the array as an argument.

You could make Field global, declaring it in a header that you #include in each file that needs it, or you could give the Animals class an int* member and make that point to Field (pass Field to each animal in the constructor.

You need to pass the array as an argument to the function...when you call the function from main, give it the array as an argument.

How do i do that? It's an array of pointers to organisms objects.

This article has been dead for over six months. Start a new discussion instead.