你能找到我的错误吗?程序编译,但运行时会崩溃并说出分段错误:第五行之后的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,其中包含main:chess.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;
}
答案 0 :(得分:0)
这样:
pieces[board_height][board_width] = NULL;
应该是这样的:
pieces[i][j] = NULL;
下次还要用-Wall编译它。它应该警告你我和j没有被使用。