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;iboard
是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;iboard
是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;iboard
是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)片的成员函数