调用类构造函数会导致段错误(c++)

Calling class constructor causes segfault (C++)

本文关键字:c++ 错误 段错误 构造函数 调用      更新时间:2023-10-16

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

主要文件:

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

我从构造函数内部调用一些函数来初始化未使用的变量,但是不管是否包含这些变量,程序都会崩溃。

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

  Piece *board[SIZE][SIZE];

,然后继续在Game::Game()中使用board,就像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);
      }
   }
}

我建议使用对象数组。然后,您将不必担心处理内存分配和回收。如果您使用指针数组,请阅读《三的法则》。

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

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的博士。