I'm programming a board game. When I call the constructor (with parameters) for the game, the program segfaults.
Main file:
#include <iostream>
#include "game.h"
using namespace std;
int main(){
int p_count = 2;
Game g(p_count);
//g.play();
}
Game Header:
#ifndef GAME_H_
#define GAME_H_
#include <iostream>
#include "board.h"
#include "player.h"
using namespace std;
class Game{
private:
Board b;
int moves, gamers;
Player players[10];
bool running;
public:
Game (int p_count);
void setup ();
void play ();
void report_score ();
bool in_mate (Player p);
bool in_check (Player p);
};
Game Constructor:
#include "game.h"
Game::Game(int p_count){
running = true;
moves = 0;
gamers = p_count;
}
Board header
#ifndef BOARD_H_
#define BOARD_H_
#include <iostream>
using namespace std;
class Piece;
class Board{
private:
static const int SIZE = 8;
Piece *board[SIZE][SIZE];
public:
Board ();
};
#endif /* BOARD_H_ */
Board constructor
#include "board.h"
#include "piece.h"
Board::Board(){
bool b = false;
for (int i=0; i<SIZE; i++){
for(int j=0; j<SIZE;j++){
board[i][j]->set_status(b);
}
}
}
Player header
#ifndef PLAYER_H_
#define PLAYER_H_
#include <iostream>
#include "board.h"
#include "piece.h"
using namespace std;
class Player{
private:
static const int NUM = 16;
Piece pieces[NUM];
int side;
public:
Player ();
Player (int p);
#endif
Player constructor
#include "player.h"
Player::Player(){
side = 0;
}
Piece header
#ifndef PIECE_H_
#define PIECE_H_
#include <iostream>
#include "board.h"
using namespace std;
class Board;
struct location{
int row;
int col;
};
class Piece{
private:
location pos_moves[50], loc;
char type;
bool status;
int moved, team;
public:
Piece ();
Piece (int piece_num, int bel);
void set_status (bool b);
};
#endif /* PIECE_H_ */
Piece implementation
#include "piece.h"
Piece::Piece(){
status = false;
team = 0;
moved = 0;
type = 'A';
}
void Piece::set_status(bool b){
status = b;
}
I call some functions from within the constructor that initialize the unused variables, but the program crashes regardless of whether or not they're included.