OK thanks a lot. It seems like I'm beginning to have another problem now though. I am getting multiple definition errors of Organism::grid inside the constructors of the classes derived from Organism.
critters.o: In function `Ant':
/home/jack/Problem_Solving_With_C++_Projects/Projects/15-11/critters.cpp:8: multiple definition of `Organism::grid'
organism.o:/home/jack/Problem_Solving_With_C++_Projects/Projects/15-11/organism.cpp:7: first defined here
main.o: In function `main':
/home/jack/Problem_Solving_With_C++_Projects/Projects/15-11/main.cpp:10: multiple definition of `Organism::grid'
organism.o:/home/jack/Problem_Solving_With_C++_Projects/Projects/15-11/organism.cpp:7: first defined here
collect2: ld returned 1 exit status
Ant is a class derived from Organism with its implementation in critters.cpp.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#ifndef ORGANISM_H
#define ORGANISM_H
#include <vector>
using namespace std;
const int FIELD_SIZE = 20;
struct Tuple
{
Tuple();
Tuple(int the_x, int the_y);
int x, y;
bool operator ==(const Tuple &rhs);
};
class Organism
{
public:
Organism();
Organism(int x_coord, int y_coord);
int get_x_coord() {return coords.x;}
int get_y_coord() {return coords.y;}
char get_id() {return id;}
void die() {alive = false;}
bool is_alive();
void update_alive();
void move();
vector<Tuple> neighbors();
static char grid[FIELD_SIZE][FIELD_SIZE];
static void init_grid();
protected:
char id;
bool alive;
Tuple coords;
};
char Organism::grid[FIELD_SIZE][FIELD_SIZE];
#endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include <iostream>
#include <vector>
#include <cstdlib>
#include "organism.h"
using namespace std;
Tuple::Tuple() : x(0), y(0)
{
//leave blank
}
Tuple::Tuple(int the_x, int the_y) : x(the_x), y(the_y)
{
//leave blank
}
bool Tuple::operator ==(const Tuple &rhs)
{
return ((x == rhs.x) && (y == rhs.y));
}
void Organism::init_grid()
{
//Initializing static member 'grid'
for (int i = 0; i < FIELD_SIZE; i++)
for (int j = 0; j < FIELD_SIZE; j++)
grid[i][j] = '-';
//--------------------------
}
Organism::Organism()
{
id = 'z';
alive = true;
coords.x = rand() % 20;
coords.y = rand() % 20;
grid[coords.y][coords.x] = id;
}
Organism::Organism(int x_coord, int y_coord)
{
id = 'z';
alive = true;
coords.x = x_coord;
coords.y = y_coord;
grid[coords.y][coords.x] = id;
}
void Organism::move()
{
vector<Tuple> the_neighbors = neighbors(); //returns neighboring coordinates
vector<Tuple> valid_choices;
Tuple valid_choice;
bool valid;
for (unsigned int i = 0; i < the_neighbors.size(); i++)
{
valid = true;
if ((grid[the_neighbors[i].y][the_neighbors[i].x] != '-')
&& (the_neighbors[i].y != coords.y || the_neighbors[i].x != coords.x))
valid = false;
if (valid)
{
valid_choice.y = the_neighbors[i].y;
valid_choice.x = the_neighbors[i].x;
valid_choices.push_back(valid_choice);
}
}
if (valid_choices.size() > 0)
{
grid[coords.y][coords.x] = '-'; // remove previous location;
int randomizer = rand() % valid_choices.size(); //randomizes choice of next valid move.
coords.y = valid_choices[randomizer].y;
coords.x = valid_choices[randomizer].x;
grid[coords.y][coords.x] = id;
}
}
vector<Tuple> Organism::neighbors()
{
vector<Tuple> neighbors;
neighbors.push_back(Tuple(coords.x, coords.y-1));
neighbors.push_back(Tuple(coords.x-1, coords.y));
neighbors.push_back(Tuple(coords.x+1, coords.y));
neighbors.push_back(Tuple(coords.x, coords.y+1));
neighbors.push_back(Tuple(coords.x, coords.y));
for (unsigned int i = 0; i < neighbors.size(); i++) //gets rid of out-of-bounds neighbors
if ((neighbors[i].x > 19) || (neighbors[i].y > 19) || (neighbors[i].x < 0) || (neighbors[i].y < 0))
{
neighbors.erase(neighbors.begin() + i);
i--;
}
return neighbors;
}
bool Organism::is_alive()
{
return alive;
}
void Organism::update_alive()
{
if (grid[coords.y][coords.x] != id)
alive = false;
}
|
And in main() I call Organism::init_grid()
Is there anything wrong with the way I'm going about this that would cause multiple definition errors?