2014-11-24 78 views
-4

你能找到我的錯誤嗎?該程序編譯,但運行時會崩潰並說分段錯誤:第五行後11。該程序在3個文件中。我正在製作一款國際象棋遊戲,現在它所要做的就是打印棋盤。這是儘可能獲取:國際象棋遊戲:分割錯誤錯誤。你能找到它嗎?

cts/Software/Chess\ Game/a.out ; exit; 
    setup complete 
    rkb*qbkr 
    pppppppp 
    ________ 
    ________ 
    ________ 
    Segmentation fault: 11 
    logout 

    [Process completed] 

文件1:board.h

/******************************* 
    * 
    * Matthew Buchanan 
    * 
    * Bocan Projects 
    * 
    * 13:11 21 November 2014 
    * 
    * board.h 
    * 
    *******************************/ 

    #include <iostream> 

    using namespace std; 

    #include "piece.h" 

    const int board_width = 8; 
    const int board_height = 8; 

    class board 
    { 
private: 
    int square[board_height][board_width]; 
    piece *pieces[board_height][board_width]; 
    int make = 0; 
    void print_square(); 
public: 
    void setup(); 
    void print(); 
    void update(); 
    }; 

    void board::print_square() 
    { 
cout << "_"; 
    } 

    void board::setup() 
    { 
if(make == 1)      //prevents setup function from being ran more than once 
    return; 
else 
{ 
    for(int i = 0; i < board_height; i++) 
    { 
     for(int j = 0; j < board_width; j++) 
     { 
      pieces[board_height][board_width] = NULL; 
     } 
    } 
    for(int i = 0; i < board_width; i++) 
    { 
     pieces[6][i] = new piece(1, 1); 
     pieces[1][i] = new piece(1, 2); 
    } 
    pieces[0][0] = new piece(2, 2); 
    pieces[0][1] = new piece(3, 2); 
    pieces[0][2] = new piece(4, 2); 
    pieces[0][3] = new piece(5, 2); 
    pieces[0][4] = new piece(6, 2); 
    pieces[0][5] = new piece(4, 2); 
    pieces[0][6] = new piece(3, 2); 
    pieces[0][7] = new piece(2, 2); 
    pieces[7][0] = new piece(2, 1); 
    pieces[7][1] = new piece(3, 1); 
    pieces[7][2] = new piece(4, 1); 
    pieces[7][3] = new piece(6, 1); 
    pieces[7][4] = new piece(5, 1); 
    pieces[7][5] = new piece(4, 1); 
    pieces[7][6] = new piece(3, 1); 
    pieces[7][7] = new piece(2, 1); 

} 
make = 1; 
    } 

    void board::print() 
    { 
for(int i = 0; i < board_height; i++) 
{ 
    for(int j = 0; j < board_width; j++) 
    { 
     if(pieces[i][j] == NULL) 
     { 
      print_square(); 
     } 
     else 
     { 
      pieces[i][j] -> print(); 
     } 
    } 
    cout << endl; 
} 
    } 

    void board::update() 
    { 
return; 
    } 

文件2:piece.h

/******************************* 
    * 
    * Matthew Buchanan 
    * 
    * Bocan Projects 
    * 
    * 13:11 21 November 2014 
    * 
    * piece.h 
    * 
    * Types: 
    * 1 - Pawn 
    * 2 - Rook 
    * 3 - Knight 
    * 4 - Bishop 
    * 5 - King 
    * 6 - Queen 
    * 
    *******************************/ 

    class piece 
    { 
private: 
    int type; 
    int team; 
public: 
    piece(); 
    piece(int, int); 
    void set_type(int); 
    void set_team(int); 
    int get_type(); 
    int get_team(); 
    void print(); 
    //void move(); the location and the move functions will be kept by the board class in board.h 
    }; 

    piece::piece() 
    { 
type = 0;    //0 is the null type 
team = 0;    //0 is the null type 
    } 

    piece::piece(int t, int c) 
    { 
type = t; 
team = c; 
    } 

    void piece::set_type(int t) 
    { 
type = t; 
    } 

    void piece::set_team(int c) 
    { 
team = c; 
    } 

    int piece::get_type() 
    { 
return type; 
    } 

    int piece::get_team() 
    { 
return team; 
    } 

    void piece::print() 
    { 
if(type == 1 && team == 1) 
    cout << "P"; 
if(type == 1 && team == 2) 
    cout << "p"; 
if(type == 2 && team == 1) 
    cout << "R"; 
if(type == 2 && team == 2) 
    cout << "r"; 
if(type == 3 && team == 1) 
    cout << "K"; 
if(type == 3 && team == 2) 
    cout << "k"; 
if(type == 4 && team == 1) 
    cout << "B"; 
if(type == 4 && team == 2) 
    cout << "b"; 
if(type == 5 && team == 1) 
    cout << "^"; 
if(type == 5 && team == 2) 
    cout << "*"; 
if(type == 6 && team == 1) 
    cout << "Q"; 
if(type == 6 && team == 2) 
    cout << "q"; 
    } 

和文件3,其中包含主:國際象棋.cpp

/********************************* 
    * 
    * Matthew Buchanan 
    * 
    * Bocan Projects 
    * 
    * 16:24 18 November 2014 
    * 
    * Chess Game 
    * 
    *********************************/ 

    #include <iostream> 

    using namespace std; 

    #include "board.h" 

    int main() 
    { 
board chess_game; 
chess_game.setup(); 
cout << "setup complete" << endl; 
chess_game.print(); 
cin.ignore(); 
return 0; 

    } 
+1

而當您使用調試器運行它時,哪條線會給出錯誤? – 2014-11-24 01:03:14

+3

爲什麼我要找到你的錯誤?這是獎品問題嗎? – JustSid 2014-11-24 01:04:06

+1

由於棋子不是多形的,棋盤的大小是固定的,所以應該使用'棋子[board_height] [board_width]'。有一個空方塊的特殊片類型。 – 2014-11-24 01:07:41

回答

0

這個:

pieces[board_height][board_width] = NULL; 

應該是這樣的:

pieces[i][j] = NULL; 

而且下一次與-Wall編譯。它應該警告你我和j沒有被使用。