Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 调用类构造函数会导致segfault(C+;+;)_C++_Constructor - Fatal编程技术网

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

C++ 调用类构造函数会导致segfault(C+;+;),c++,constructor,C++,Constructor,我正在编写一个棋盘游戏。当我为游戏调用构造函数(带参数)时,程序会出错 主文件: #include <iostream> #include "game.h" using namespace std; int main(){ int p_count = 2; Game g(p_count); //g.play(); } 板头 #ifndef BOARD_H_ #define BOARD_H_ #include <iostream> us

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

主文件:

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

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

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

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

我从构造函数中调用了一些函数来初始化未使用的变量,但是无论是否包含这些变量,程序都会崩溃

board
块的指针的二维数组
,在
board
的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);
        }
    }
}
Board::Board(){
布尔b=假;

对于(int i=0;i
board
Piece
指针的二维数组,在
board
的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);
        }
    }
}
Board::Board(){
布尔b=假;

对于(int i=0;i
board
Piece
指针的二维数组,在
board
的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);
        }
    }
}
Board::Board(){
布尔b=假;

对于(int i=0;i
board
Piece
指针的二维数组,在
board
的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);
        }
    }
}
Board::Board(){
布尔b=假;

对于(int i=0;i我看到的一个问题是,您将
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::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::Board(){
布尔b=假;

对于(int i=0;i我看到的一个问题是,您将
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::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::Board(){
布尔b=假;

对于(int i=0;i我看到的一个问题是,您将
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::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::Board(){
布尔b=假;

对于(int i=0;i我看到的一个问题是,您将
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::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::Board(){
布尔b=假;


对于(inti=0;我怎么做
Player
Board
构造器呢?Player就是我提到的在构造器中调用的,它调用Board,它们之间做了很多事情来设置游戏。但是就像我说的,从构造器中注释它们仍然会导致它出错,所以我认为它们不是问题的根源即使在注释显式初始化时,rs也会被隐式调用
在构造函数的主体中,在此之前它仍然会调用默认构造函数。你应该使用。哦,我没有想到。所以我可能也应该检查那些构造函数?你需要向我们展示
Board
Player
的默认构造函数。
Player
Board
construct的默认构造函数tors做什么?我提到的玩家是在构造函数中调用的,它调用棋盘,它们之间做了很多事情来设置游戏。但是就像我说的,在构造函数中注释它们仍然会导致它出错,所以我认为它们不是问题的根源。即使在注释显式初始化时,它们的构造函数也会被隐式调用n out。还要注意,如果使用
b=Board(…)初始化它们
在构造函数的主体中,在此之前它仍然会调用默认构造函数。你应该使用。哦,我没有想到。所以我可能也应该检查那些构造函数?你需要向我们展示
Board
Player
的默认构造函数。
Player
Board
construct的默认构造函数tors做什么?我提到的玩家是在构造函数中调用的,它调用棋盘,它们之间做了很多事情来设置游戏。但是就像我说的,在构造函数中注释它们仍然会导致它出错,所以我认为它们不是问题的根源。即使在注释显式初始化时,它们的构造函数也会被隐式调用n out。还要注意,如果使用
b=Board(…)初始化它们
在构造函数的主体中,在此之前它仍然会调用默认构造函数。你应该使用。哦,我没有想到。所以我可能也应该检查那些构造函数?你需要向我们展示
Board
Player
的默认构造函数。
Player
Board
construct的默认构造函数tors做什么?我提到的玩家是在构造函数中调用的,它调用棋盘,它们之间做了很多事情来设置游戏。但是就像我说的,在构造函数中注释它们仍然会导致它出错,所以我认为它们不是问题的根源。即使在注释显式初始化时,它们的构造函数也会被隐式调用n out。还要注意,如果使用
b=Board(…)初始化它们
在构造函数的主体中,在此之前它仍然会调用默认构造函数。你应该使用。哦,我没有想到。所以我可能也应该检查这些构造函数?你需要向我们展示
板和
播放器的默认构造函数。我也没有看到设置状态(bool)片的成员函数