Getter and Segmentation Fault

Getter and Segmentation Fault

本文关键字:Fault Segmentation and Getter      更新时间:2023-10-16

我的程序需要一些帮助:

#include <iostream>
#include<conio.h>
using namespace std;

class Position {
public :
    int             line;
    int             column;
                    Position(int,int);
                    Position();
};
Position::Position(int n, int m) : line{n},column{m}{}
class Board {
private :
    int**           tab;
    int             nbline;
    int             nbcolumn;
public  :
                    Board(int, int);
    void            setValue(Position&, int);
    int             getValue(Position&);
    int             getNbline();
    int             getNbcolumn();
};

class Play {
private :
 // Play         m_instance;
    void            moves(Board, Position&); // quand le joueur joue
 // void            moves(Board, Position); // quand l'IA joue. Mettre une énum pour direction,
    bool            wincondition(Board);
    Play&           operator=(const Play&);
public :
// static Play&    Instance();
};
Board::Board(int m, int n) : tab{new int*[m]}, nbline{m}, nbcolumn{n}{
   int  x(0);
   for (int i = 0; i < m; ++i){
        tab[i] = new int[n];
        for(int j = 0; j < n; ++j) {
            tab[i][j] = x; x++;}}
}

void     Board::setValue(Position& p, int value)         { tab[p.line][p.column] = value; }
int      Board::getValue(Position& p)                    { return tab[p.line][p.column];  }
int      Board::getNbline()                              { return nbline;                 }
int      Board::getNbcolumn()                            { return nbcolumn;               }

void Play::moves(Board tab, Position& blank) {
   /* int             c = getch() ;
    Position        tmp;
    if(c==0 || c==224) {c = getch();

    switch(c){
    case 75 : //left
        if(blank.column-1>=0) {
                tmp.column = blank.column;
                tmp.line   = blank.line;
                tab.setValue(blank,tab.getValue(tmp));
                blank.column++;
                tab.setValue(blank, 0);
                }
        break;
    case 72 : // haut
        if(blank.line+1<=0) {
                tmp.column = blank.column+1;
                tmp.line   = blank.line;
                tab.setValue(blank,tab.getValue(tmp));
                blank.column++;
                tab.setValue(blank, 0);
            }
        break;

    case 77 ://droit
        if(blank.column+1<=tab.getNbcolumn()) {
                tmp.column = blank.column;
                tmp.line   = blank.line;
                tab.setValue(blank,tab.getValue(tmp));
                blank.column--;
                tab.setValue(blank, 0);
            }
        break;
    case 80 : //bas
        if(blank.line+1<=tab.getNbline()) {
                tmp.column = blank.column+1;
                tmp.line   = blank.line;
                tab.setValue(blank,tab.getValue(tmp));
                blank.column++;
                tab.setValue(blank, 0);
            }
        break;
    default : cout << "n ERROR " << endl; break; // erreur
    }
}*/
}
int main()
{
    int             lines, columns;

    cout << "Enter number of lines" << endl;
    cin >> lines;
    cout << "Enter number of columns" << endl;
    cin >> columns;
    Board           tab(lines,columns);
    Position        pos(lines,columns);
    Position&       p = pos;

    for (int i = 0; i<lines;i++)
    {
        for(int j = 0; j<columns;j++)
        {
            cout << tab.getValue(p) << " ";
            if (i == lines) { cout << endl;}
        }
    }
    return 0;
}

当我在第139行调用getValue时,我得到一个分段错误。Get值在第57行定义。在执行getValue时,p.linep.column都获得了在主函数开头捕获的正确值。

该程序没有错误,只有两个警告,因为我没有使用Play::moves参数(因为目前在/* */之间,正在等待测试)。我使用代码::带有-Wall -Wextra -pedantic -std=c++11的块。

我真的认为没有理由出现分割错误。我错过什么了吗?

您调用get的位置设置为您的板的大小。由于数组是基于0索引的,因此数组的大小实际上是数组末尾的一倍。

const int size = 100
int arr[size];  //0, 1, 2, ... 98, 99
arr[size];  // fail arr is 0-99 and size is 100
相关文章: