줄에 segfault가 생겨 복사 생성자와 함께 C++ 문자열을 만듭니다. 비슷한 문제를 살펴 보았지만 모두 나쁜 C++ 문자열 객체를 전달할 예정입니다. 난 그냥 원시 문자열을 전달 해요, 그래서 내 문제가 뭔지 모르겠다. 코드의 관련 스 니펫을 붙여 넣을 것입니다 (여러 파일에서 가져온 것이므로 조금 뒤죽박죽이 될 수 있습니다). segfault는 Species 클래스의 기본 생성자의 네 번째 줄에서 발생합니다.문자열의 복사 생성자에 Segfault
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();
};
, 종이 기록 소문자, C++은 대소 문자를 구분합니다. (이번에는 코멘트로 게시 됨) –