簡體   English   中英

調用類構造函數會導致段錯誤(C ++)

[英]Calling class constructor causes segfault (C++)

我正在編寫一個棋盤游戲。 當我調用游戲的構造函數(帶有參數)時,程序出現段錯誤。

主文件:

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

我建議使用對象數組。 這樣,您就不必擔心處理內存分配和釋放。 如果使用指針數組,請閱讀《三則規則》

board是指針的2-d陣列Piece ,你應該new之前在使用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中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM