字符串的复制构造函数上的隔离错误

Segfault on copy constructor for string

本文关键字:隔离 错误 构造函数 复制 字符串      更新时间:2023-10-16

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

Species::Species(string _type) {
    program_length = 0;
    cout << _type << " 1n";
    cout << type << " 2n";
    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();
};

问题最可能的原因是您在未初始化的情况下取消引用的指针数据成员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") 
{ 
  ....
}