2014-09-03 76 views
0

我想做一個國際象棋遊戲,我剛開始學習C++,我有一個basedice類,我從它派生了我的骰子。該程序崩潰後,我想訪問我的作品在gamepos數組中的一件。在knight.cpp中的mousepressevent函數崩潰發生。也請關於我的英語。C++程序在給二維數組指針賦值後崩潰

我的代碼是:

basedice.h

class basedice:public QGraphicsPixmapItem 
{ 
public: 
    basedice(); 
    enum KIND {Kinght,Rook,Pawn,Bishop,Queen,King}; 
    KIND kind; 
    enum COLOR {white,black}; 
    COLOR color; 

    void setposition(); 

    basedice *gamepos[8][8]; 

}; 

basedice.cpp

basedice::basedice() 
{ 
} 

void basedice::setposition() 
{ 
    for (int i=0;i<8;i++) 
    { 
     for (int j=0;j<8;j++) 
      { 
      gamepos[i][j]=NULL; 
      } 
    } 
} 

board.h

class board:public basedice 
{ 
public: 
    board(); 
    void additem(QGraphicsScene *basescene); 
    board *_board; 

}; 

board.cpp

board::board() 
    { 
    setZValue(0); 
    } 

    void board::additem(QGraphicsScene *basescene) 
    { 
    setposition(); 
    _board= new board; 
    QPixmap boarditem (":/images/board.png"); 
    _board->setPixmap(boarditem); 
    _board->setPos(0,0); 

    basescene->addItem(_board); 
    } 

knight.h

class knight:public basedice 
{ 
public: 
    knight(); 
    void additem(QGraphicsScene *basescene); 
    QVector <knight *>_blackknight; 
    QVector <knight *> _whiteknight; 
    void mousePressEvent(QGraphicsSceneMouseEvent *event); 

} 

knight.cpp

knight::knight() 
{ 
setFlag(ItemIsMovable); 
setZValue(1); 
} 

void knight::additem(QGraphicsScene *basescene) 
{ 

    _blackknight.resize(2); 
    _whiteknight.resize(2); 
    for (int i=0;i<2;i++) 
    { 
     _blackknight[i]=new knight;  
     _whiteknight[i]=new knight; 
    } 

    QPixmap bknight(":/images/basb.png"); 
    for (int i=0;i<2;i++) 
    { 

     _blackknight[i]->setPixmap(bknight); 
     _blackknight[i]->setPos(140+i*5*140,0); 
     _blackknight[i]->color=black; 
     _blackknight[i]->kind=Kinght; 
     gamepos[1+i*5][0]=(_blackknight[i]); 


     basescene->addItem(_blackknight[i]); 

    } 

    QPixmap wknight(":/images/wasb.png"); 
    for (int i=0;i<2;i++) 
    { 

     _whiteknight[i]->setPixmap(wknight); 
     _whiteknight[i]->setPos(140+i*5*140,980); 
     _whiteknight[i]->color=white; 

     _whiteknight[i]->kind=Kinght; 
     gamepos[1+i*5][7]=(_whiteknight[i]); 
     basescene->addItem(_whiteknight[i]); 

    } 


} 


void knight::mousePressEvent(QGraphicsSceneMouseEvent *event) 
{ 

    for (int i=0;i<8;i++) 
    { 
     for (int j=0;j<8;j++) 
     { 
     qDebug()<<gamepos[i][j];//this cause the program to crash 
     } 
    } 
} 

而且chessscene只是爲了顯示我怎麼稱呼他們: chessscene.h

class chessscene 
{ 
public: 
    chessscene(); 
    QGraphicsScene *basescene; 

}; 

chessscene。 cpp

chessscene::chessscene() 
{ 
    basescene=new QGraphicsScene; 

    board _board; 
    knight _knight; 

    _board.additem(basescene); 
    _knight.additem(basescene); 

}

+0

究竟是哪條線墜毀? – MrEricSir 2014-09-03 23:01:27

+0

在knight.cpp ..中mousepressevent函數'qDebug()<< gamepos [i] [j]' – behrooz 2014-09-03 23:46:42

+0

我的調試器表示它是從指針的內部它。 – behrooz 2014-09-03 23:48:10

回答

1

我想你可能會有點困惑,誰擁有你的程序有什麼變數。你的問題是,你沒有按照你認爲你在使用基類的basedice *gamepos[8][8];做什麼。

你在做什麼是創造指針的8×8矩陣bacedice小號爲每一個從basedice繼承類。 _board有它自己的矩陣。每個騎士都有自己的矩陣:_blackknight_whiteknight向量中的每個項目都有其自己的單獨矩陣。

您將此矩陣初始化爲setposition(),但您只能在board類上調用該矩陣。之後,當您嘗試爲knight實例之一調試gamepos時,其gamepos矩陣尚未初始化。這些矩陣並不是簡單地共享的,因爲繼承者都是從同一個類繼承而來,相反,它們對於每個實例都是獨立的。

換句話說,不要把gamepos矩陣放在basedice。相反,作爲一種替代方案,如果他們需要訪問board擁有的數據,則可以將其單獨放入board類別中,並將您的board實例作爲指向您的knight的指針。您最好在board上創建方法,將變量封裝在您的board實例中,以便例如騎士可以詢問董事會所在的位置,並且根本不必知道gamepos的存在。

最後,在我看來,你不應該從basediceQGraphicsPixmapItem繼承board可言,但或許直接。繼承意味着父類中的所有事情在繼承類中都是真實的。實際上,你的意思是,每塊板都有一個種類,一個顏色和一個位置矩陣,每一塊都有一個種類,一個顏色和一個位置矩陣。 KIND和COLOR對board沒有任何意義,而位置矩陣在你的作品中沒有多大意義。