调用类构造函数会导致段错误(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
是Piece
的指针的二维数组,在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
的博士。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'