2013-11-03 3 views
0

줄에 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(); 
}; 
+0

, 종이 기록 소문자, C++은 대소 문자를 구분합니다. (이번에는 코멘트로 게시 됨) –

답변

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") 
{ 
    .... 
} 
+0

포인터를 사용하지 않도록 변경했습니다. 이제는 더 이상한 오류가 발생합니다 : "오류 : 필드 벽이 Grid의 멤버가 아닙니다." 내 원래 게시물에서 내 코드를 업데이트하겠습니다. – zaloo

+0

@ user2756569 예제를 추가했습니다. 나는 C++ 서적을 읽을 것을 제안한다. – juanchopanza

+0

벽 초기화 및 비어있는 초기화 목록을 현재 목록으로 이동하는 방법을 이해하지 못합니다. 컴파일러는 여전히 wall/empty가 제안한 변경 후에 그리드의 멤버가 아니라고 분명히 말합니다. – zaloo