c++ - 字符串复制构造函数的段错误

标签 c++ string segmentation-fault copy-constructor

我在使用复制构造函数创建 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::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") 
{ 
  ....
} 

关于c++ - 字符串复制构造函数的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19758474/

相关文章:

C++ 通过引用传递 : error: no matching function for call

javascript - 使用插值标记分割字符串

c - bsearch() 返回 (nil),导致段错误

C++使随机引擎静态

c++ - 如何避免获取标准库函数地址时的非标准行为

c++ - 如何在接受用户输入时运行相同的功能

C 填充二维数组

c++ - 使用 gdb 和 codelite 调试 C++ 中的无限循环

c - 如何从用户接收字符串以接收空字符串并在c中打印它们

c++ - 数组字符串转换时出现段错误