0

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 by SpyrosMet: n/a

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by SpyrosMet
0

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.

0

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.

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.