0

我在使用复制构造函数创建 c++ 字符串的行上遇到了段错误。我已经查看了一些类似的问题,但它们都是由于传入了错误的 c++ 字符串对象。我只是传入一个原始字符串,所以我不确定我的问题是什么。我将粘贴相关的代码片段(它取自几个不同的文件,因此看起来可能有点混乱)。段错误发生在 Species 类的默认构造函数的第 4 行。

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();
};

这是我从墙上删除指针并清空后更新的代码。我现在收到一个奇怪的错误(“错误:外墙不是网格的成员”):

Grid::Grid(int _width, int _height) {
    (Grid::wall) = Species("wall");
        (Grid::empty) = Species("empty");
        cout << (*wall).getType() << "\n";
        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(Grid::empty,a,b,NORTH,this);
                (Grid::map)[a][b] = empty_creature;
            }
}

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();
};
4

1 回答 1

3

问题的最可能原因是Species您在未初始化的情况下取消引用的指针数据成员:

Grid::Grid(int _width, int _height) {
  *wall = Species("wall");  // wall not initialized. What does it point to?
  *empty = Species("empty"); // likewise for empty

这就引出了一个问题:你是否需要它们作为指针?(提示:很可能不是)

不使用指针时,您可以轻松地在构造函数的初始化列表中初始化数据成员:

Grid::Grid(int _width, int _height) 
: width(_width), height(_height), turn_number(0), wall("wall"), empty("empty") 
{ 
  ....
} 
于 2013-11-03T21:41:47.523 回答