I'm getting a segfault on a line where I'm creating a c++ string with the copy constructor. I've looked at some of the similar issues, but they're all due to passing in a bad c++ string object. I'm just passing in a raw string, so I'm not sure what my issue is. I'll paste the relevant snippets of code (it's taken from several different files, so it might look a bit jumbled). The segfault occurs in the 4th line of the default constructor for the Species class.
Species::Species(string _type) {
program_length = 0;
cout << _type << " 1\n";
cout << type << " 2\n";
type = string(_type);
}
Grid::Grid(int _width, int _height) {
*wall = Species("wall");
*empty = Species("empty");
turn_number = 0;
width = _width;
height = _height;
for(int a= 0; a < 100; a++)
for(int b = 0; b< 100; b++) {
Creature empty_creature = Creature(*empty,a,b,NORTH,this);
(Grid::map)[a][b] = empty_creature;
}
}
int main() {
Grid world = Grid(8,8);
}
class Grid {
protected:
Creature map[100][100];
int width,height;
int turn_number;
Species *empty;
Species *wall;
public:
Grid();
Grid(int _width, int _height);
void addCreature(Species &_species, int x, int y, Direction orientation);
void addWall(int x, int y);
void takeTurn();
void infect(int x, int y, Direction orientation, Species &_species);
void hop(int x, int y, Direction orientation);
bool ifWall(int x, int y, Direction orientation);
bool ifEnemy(int x, int y, Direction orientation, Species &_species);
bool ifEmpty(int x, int y, Direction orientation);
void print();
};
class Species {
protected:
int program_length;
string program[100];
string type;
public:
species(string _type);
void addInstruction(string instruction);
bool isWall();
bool isEmpty();
bool isEnemy(Species _enemy);
string instructionAt(int index);
string getType();
};