调用类构造函数导致segfault(C ++)

时间:2015-07-27 01:37:55

标签: c++ constructor

我正在编写棋盘游戏。当我为游戏调用构造函数(带参数)时,程序会出现段错误。

主档案:

#include <iostream>
#include "game.h"
using namespace std;

int main(){
    int p_count = 2;
    Game g(p_count);
    //g.play(); 
}

游戏标题:

#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);
};

游戏构造函数:

#include "game.h"

Game::Game(int p_count){
    running = true;
    moves = 0;
    gamers = p_count;
    }

董事会标题

#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_ */

董事会建设者

#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);
        }
    }
}

玩家标题

#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

玩家构造函数

#include "player.h"
Player::Player(){
    side = 0;
}

片头

#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_ */

片段实施

#include "piece.h"
Piece::Piece(){
    status = false;
    team = 0;
    moved = 0;
    type = 'A';
}

void Piece::set_status(bool b){
    status = b;
}

我在构造函数中调用一些初始化未使用变量的函数,但程序崩溃,无论它们是否包含在内。

2 个答案:

答案 0 :(得分:3)

我看到的一个问题是你已将board定义为一个指针数组,而不是对象,

  Piece *board[SIZE][SIZE];

然后继续使用board中的Game::Game(),就像board指向有效对象一样。

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);
      }
   }
}

您可以通过使board成为一组对象来解决这个问题。

  Piece board[SIZE][SIZE];

或确保在使用之前为数组的每个元素分配内存。

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);
      }
   }
}

我建议使用一组对象。然后,您不必担心处理内存分配和释放。如果您使用指针数组,请阅读The Rule of Three

答案 1 :(得分:0)

boardPiece指针的二维数组,在new的ctor中使用它之前,您应该Board

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);
        }
    }
}
顺便说一句,不要忘记delete指针,也许在Board的dtor中。