字符串的复制构造函数上的隔离错误
Segfault on copy constructor for string
我在使用复制构造函数创建 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")
{
....
}
相关文章:
- 从矢量中删除元素后出现隔离错误
- 在 gtest 中初始化堆栈上的引用变量的隔离错误
- 线程时访问静态映射时出现隔离错误
- 并行快速排序分区中的隔离错误
- C++多线程程序:变量定义为类成员的隔离错误
- TFLite 隔离错误,通过获取C++输入和输出
- 我只是在寻找模板,在我的书中找到了这段代码,这显示了隔离错误?
- pthread_create中错误 4 的隔离错误
- 递归树遍历/分支删除的隔离错误
- 在类模板上使用 arm gcc 编译期间的隔离错误
- 从大量文件读取时出现隔离错误
- 在PHP扩展中使用emalloc从线程时出现隔离错误
- 通过 Boost Python 在C++对象之间传递共享指针的隔离错误
- 在QT中单击菜单时出现隔离错误
- 尝试访问标头声明成员时出现隔离错误
- 搜索链表时出现隔离错误
- 模板化子类析构函数中的隔离错误
- 插件中节点.js/Nan 回调C++不频繁的隔离错误
- 将行添加到 GTKTreeView 时的隔离错误
- 找不到命令时打开的隔离错误