C++ 分割错误:11-它在哪里?

C++ 分割错误:11-它在哪里?,c++,segmentation-fault,C++,Segmentation Fault,我正在用gcc编译一个程序,当我运行a.out时,我得到以下错误: 分段错误:11 我认为这与Board::display有关,但我看不出有什么问题 #include <iostream> using namespace std; const bool WHITE = 0; const bool BLACK = 1; //----------------------------- class Piece { public: // constructors Pi

我正在用gcc编译一个程序,当我运行
a.out
时,我得到以下错误:

分段错误:11

我认为这与Board::display有关,但我看不出有什么问题

#include <iostream>

using namespace std;

const bool WHITE = 0;
const bool BLACK = 1;

//-----------------------------

class Piece
{
public:
    // constructors
    Piece();
    Piece(bool color);

    // functions
    char getColor()  const {return color; }
    bool getSymbol() const {return symbol;}

protected:
    bool color;
    char symbol;
};

ostream& operator << (ostream& out, const Piece & piece)
{
    out << piece.getSymbol();
    return out;
}

Piece::Piece() { }

Piece::Piece(bool color)
{
    this->color = color;
}

//-----------------------------

class King : public Piece
{
public:
    King(bool color);
};

King::King(bool color) : Piece(color)
{
    this->symbol = 'K';
}

//-----------------------------

class Tile
{
public:
    // constructors/destructors
    Tile();
    Tile(Piece piece, int row, int col);

    // functions
    bool  getColor()  const {return color;}
    void display();

private:
    Piece piece;
    int row, col;
    bool color;

};

Tile::Tile() { }

Tile::Tile(Piece piece, int row, int col)
{
    this->row = row;
    this->col = col;
}

void Tile::display()
{
    if (getColor() == BLACK)
        {
            if (piece.getColor() == BLACK)
                cout << "\E[30;47m " << piece << " \E[0m";
            else
                cout << "\E[37;47m " << piece << " \E[0m";
        }
        else
        {
            if (piece.getColor() == BLACK)
                cout << "\E[30;41m " << piece << " \E[0m";
            else
                cout << "\E[37;41m " << piece << " \E[0m";
        }
}

//---------------------------

class Board
{
public:
    // constructor
    Board();

    // functions
    void display();

private:
    Tile *tiles[8][8];
};

Board::Board()
{
    for (int r = 0; r < 8; r++)
        for(int c = 8; c < 8; c++)
            tiles[r][c] = new Tile(King(BLACK), r, c);
}

void Board::display()
{
    for (int r = 7; r >= 0; r--)
    {
        for(int c = 7; c >= 0; c--)
            tiles[r][c]->display();
        cout << endl;
    }
}

//---------------------------

int main() 
{
    Board board;

    board.display();
}
#包括
使用名称空间std;
常数布尔白=0;
常数布尔黑=1;
//-----------------------------
班级作品
{
公众:
//建设者
件();
单件(布尔色);
//功能
char getColor()常量{return color;}
bool getSymbol()常量{return symbol;}
受保护的:
颜色;
字符符号;
};
ostream&运算符符号='K';
}
//-----------------------------
类瓷砖
{
公众:
//构造函数/析构函数
瓷砖();
瓷砖(块、整行、整列);
//功能
bool getColor()常量{return color;}
void display();
私人:
一件一件;
int row,col;
颜色;
};
平铺::平铺(){}
平铺::平铺(整块、整行、整列)
{
此->行=行;
这个->列=列;
}
void Tile::display()
{
如果(getColor()==黑色)
{
if(piece.getColor()=黑色)

cout在
Board::display
中,循环中的变量是递增的,而不是递减的



您也没有在
Tile
的构造函数中将
piece
赋值给
Tile::piece
this->piece=piece

Board::display
中,您是在增加循环中的变量,而不是减少它们


您也没有在
Tile
的构造函数中将
piece
赋值给
Tile::piece
this->piece=piece

Board::display()
中,
r++
应该是
r--
,对于
c++
,也是如此

如果(像我一样)喜欢使用无符号变量作为数组索引,则可以这样编写循环:

for (std::size_t i = 0; i != N; ++i)
{
    array[N - 1 - i] = something();
}
或者,如果您觉得这很麻烦,但仍然不喜欢
Board::display()
中的
r++
应该是
r--
,对于
c++
也是如此

如果(像我一样)喜欢使用无符号变量作为数组索引,则可以这样编写循环:

for (std::size_t i = 0; i != N; ++i)
{
    array[N - 1 - i] = something();
}
或者,如果你觉得这很麻烦,但你仍然不喜欢
,你的意思肯定是:

for (int r = 7; r >= 0; r--)
{
    for(int c = 7; c >= 0; c--)
将来

你的意思当然是:

for (int r = 7; r >= 0; r--)
{
    for(int c = 7; c >= 0; c--)
将来


除非只是打字错误,否则:

for(int c = 8; c < 8; c++)

在某些情况下,您可能希望删除这些,但更好的选择可能是在这种情况下根本不使用指针,只使用
平铺平铺[8][8];
矢量平铺

,除非它只是一个键入行:

for(int c = 8; c < 8; c++)

在某些情况下,您可能会想要删除这些,尽管在本例中更好的选择可能是根本不使用指针,只需
Tile tiles[8][8]
矢量平铺

谢谢,我没有看到。但是,在修复它之后,我仍然得到了一个segfault。我将在原始问题中更改它。@AustinMoore:当你开始时,将问题浓缩到重要的部分!使用调试器找出代码的中断位置,然后发布一个问题。@AustinMooreE:请停下来,拿出一本关于C++的好书,坐下来喝咖啡几小时,然后学习更多关于对象模型、继承和虚拟函数的基本知识。这确实是一个远远超过StAcExpLoad问题的教育任务。我们很乐意帮助你解决问题,但是我们不能提供完整的语言课程。@ Marlon:但是
Piece
成员函数不是虚拟函数,
Piece
没有虚拟析构函数,
Piece
是按值存储的,@KerrekSB你是对的。在我读了你的答案后,我意识到我忽略了多少。(我已经给了你的答案a+1,非常完整!)谢谢,我没有看到。但是,在修复之后,我仍然会得到一个Sebug。我将在原来的问题中改变它。@奥斯汀摩尔:当你在这个问题上,把问题浓缩到最重要的部分!使用调试器来找出代码的断裂位置,然后发布一个问题。@奥斯汀摩尔:请停止。拿出一本关于C++的好书,坐下来喝一杯咖啡几个小时,学习更多关于对象模型、继承和虚拟函数的基础知识。这确实是一项教育任务,远远超出了StackOverflow问题。我们很乐意帮助解决问题,但我们不能提供完整的语言课程。@Marlon:但是
片段
成员函数ons不是虚拟的,
Piece
没有虚拟析构函数,
Piece
是按值存储的,@KerrekSB你是对的。在我读了你的答案后,我意识到我忽略了多少。(我已经给了你答案a+1,非常完整!)人们在下面的答案中找到了这个特殊错误的来源。但下次发生这种情况时,请尝试启动调试器(如gdb)要定位代码中的位置,就会发生这种情况。我打赌如果您查看的是精确的行,您会在循环中发现错误。人们在下面的答案中找到了此特定SEGFULT的来源。但下次发生这种情况时,请尝试启动调试器(如gdb)要定位代码中的位置,就会发生这种情况。我打赌,如果您查看的是精确的行,您会在循环中发现错误。
for(int c = 0; c < 8; c++)