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.

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.

Member Avatar for r.stiltskin

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.