Segfault对字符串的复制构造函数

时间:2013-11-03 21:39:38

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

1 个答案:

答案 0 :(得分: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") 
{ 
  ....
}