简体   繁体   中英

Calling class constructor causes segfault (C++)

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.

One problem that I see is that you have defined board as an array of pointers, not objects,

  Piece *board[SIZE][SIZE];

and then you proceed to use board in Game::Game() as though board points to valid objects.

Board::Board(){
   bool b = false;
   for (int i=0; i<SIZE; i++){
      for(int j=0; j<SIZE;j++){

         // Problem.
         // board[i][j] has not been initialized
         // to point to any valid object.
         board[i][j]->set_status(b);
      }
   }
}

You can resolve that by making board an array of objects.

  Piece board[SIZE][SIZE];

or making sure that you allocate memory each element of the array before using them.

Board::Board(){
   bool b = false;
   for (int i=0; i<SIZE; i++){
      for(int j=0; j<SIZE;j++){

         // Allocate memory for the element
         // of the array.
         board[i][j] = new Piece;
         board[i][j]->set_status(b);
      }
   }
}

I recommend using an array of objects. Then, you won't have to worry about dealing with memory allocation and deallocation. If you use an array of pointers, please read The Rule of Three .

board is a 2-d array of the pointer of Piece , you should new it before use it in Board 's ctor:

Board::Board(){
    bool b = false;
    for (int i=0; i<SIZE; i++){
        for(int j=0; j<SIZE;j++){
            board[i][j] = new Piece;  // add it here
            board[i][j]->set_status(b);
        }
    }
}

BTW, don't forget to delete the pointers, maybe in Board 's dtor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM