2015-03-30 133 views
0

我需要我的程序有點幫助:getter和分割故障

#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,我得到一個分段錯誤。獲取值在第57行定義。當執行getValue時,p.linep.column都獲得了在主函數開始處捕獲的正確值。

該程序沒有錯誤,只有2個警告,因爲我沒有使用Play::moves參數(因爲目前在/* */之間,等待測試)。我使用Code :: Blocks與-Wall -Wextra -pedantic -std=c++11

我實在看不出有什麼理由分段錯誤。我錯過了什麼?

+0

*我需要的,不是我我需要... :( – Csi 2015-03-30 19:51:26

+0

什麼是您的調試器說? – 2015-03-30 20:09:30

+0

如果你的描述包括「管線139,」你的代碼可能是太長了這種格式。啓動事實上你的代碼清單中包含一個被註釋掉的長塊,因爲LRiO說:使用調試器來檢查你的變量,如果這樣做沒有幫助,就開始刪除與錯誤無關的所有事情。你犯了一個錯誤,如果你沒有,並且已經將代碼縮減到例如十行或者易於閱讀的代碼,那麼你將有更多的機會閱讀代碼並尋找問題 – 2015-03-30 21:02:40

回答

0

要調用與設置爲您的主板的尺寸的位置得到。由於數組是基於0索引的,因此數組的大小實際上是數組的末尾。

const int size = 100 
int arr[size]; //0, 1, 2, ... 98, 99 
arr[size]; // fail arr is 0-99 and size is 100 
+0

謝謝先生!:) – Csi 2015-03-30 20:22:51