2016-03-25 15 views
0

'dan 2B dizisini görüntüledikten sonra çöküyor Benim C++ programım, 3x3 int dizisi ve iki tam sayı olan yapmamın bir "hal" ini tutması gereken Devletler adı verilen bir sınıftan oluşur. Onları ana olarak ilan ettiğimde ve yazdırdıklarında, doğru değerlerle doğru şekilde yazdırılırlar, ancak bunları bir sınıftaki sınıflardan biri tarafından sıralanan ve onu geri çekerek bir priority_queue'a yerleştirdikten sonra dizide çöp var ve çöker. görüntülemeye çalışır. Onu yerleştirdiğimde ve sonra geri çekildiğinde neden çöp aldığına dair hiçbir fikrim yok, herkes açıklayabilir mi/yardım edebilir mi?Program Crash

main.cpp:

#include "states.h" 
#include <cstdlib> 
#include <iostream> 
#include <queue> 

using namespace std; 

int main(int argc, char *argv[]) 
{ 
priority_queue<States> statesQueue; 
int puzzle[3][3]; 
for(int i=0; i<3; i++) 
    for(int j=0; j<3; j++) 
     puzzle[i][j] = i+j; 

States myPuzzle(puzzle); 
States myPuzzle2; 

myPuzzle.printPuzzle(); 
myPuzzle2.printPuzzle(); 
myPuzzle.setFOfN(5); 
myPuzzle2.setFOfN(0); 

statesQueue.push(myPuzzle); 
statesQueue.push(myPuzzle2); 

if(statesQueue.empty()==true) 
    cout<< "This is empty" <<endl; 
else 
    cout<< "This is not empty" <<endl; 

statesQueue.top().printPuzzle(); 

system("PAUSE"); 
return EXIT_SUCCESS; 
} 

States.h:

#define STATES_H 
#include <iostream> 
using namespace std; 

class States 
{ 
public: 

    States(); 
    States (int puzzle[][3]); 
    ~States(); 

    int getFOfN() const; 
    void setFOfN(int num); 

    int getGOfN() const; 
    void setGOfN(int num); 

    void incGOfN(); 
    int** getPuzzle() const; 

    void printPuzzle() const; 

    bool States::operator<(const States& rhs) const 
    { 
     return (fOfN < rhs.getFOfN()); 
    } 
    States& operator = (const States& rhs) 
    { 
     for(int i=0; i<3; i++) 
     { 
      for(int j=0; j<3; j++) 
      {  
       cout<< i << " " << j << " " << rhs.getPuzzle()[i][j] <<endl; 
       puzzleGame[i][j] = rhs.getPuzzle()[i][j]; 
      } 
     } 
    }       


private: 
    int** puzzleGame; 
    int gOfN; 
    int fOfN; 
}; 
#endif 

States.cpp:

#include "states.h" 
#include <iostream> 

States::States() 
{ 
puzzleGame = new int*[3]; 

for (int i = 0; i<3; i++) 
    puzzleGame[i] = new int[3]; 

puzzleGame[0][0] = 1; 
puzzleGame[0][1] = 2; 
puzzleGame[0][2] = 3; 
puzzleGame[1][0] = 4; 
puzzleGame[1][1] = 0; 
puzzleGame[1][2] = 5; 
puzzleGame[2][0] = 6; 
puzzleGame[2][1] = 7; 
puzzleGame[2][2] = 8; 

} 
//------------------------------------------------------------------------- 
States::States (int puzzle[][3]) 
{ 
puzzleGame = new int*[3]; 

for (int i = 0; i<3; i++) 
    puzzleGame[i] = new int[3]; 

for(int i=0; i<3; i++) 
    for(int j=0; j<3; j++) 
     puzzleGame[i][j] = puzzle[i][j]; 
} 
//------------------------------------------------------------------------- 
States::~States() 
{ 
for (int i=0; i<3; i++) 
{ 
    delete [] puzzleGame[i]; 
} 
delete [] puzzleGame; 
puzzleGame = 0; 
} 
//--------------------------------------------------------------------------  
int States::getFOfN() const 
{ 

return fOfN; 
} 
//------------------------------------------------------------------------- 
void States::setFOfN(int num) 
{ 
fOfN = num; 
} 
//--------------------------------------------------------------------------  
int States::getGOfN() const 
{ 

return gOfN; 
} 
//------------------------------------------------------------------------- 
void States::setGOfN(int num) 
{ 
gOfN = num; 
} 
//--------------------------------------------------------------------------  
void States::incGOfN() 
{ 
gOfN++; 
} 
//------------------------------------------------------------------------- 
int** States::getPuzzle() const 
{ 

return puzzleGame; 
} 
//------------------------------------------------------------------------- 
void States::printPuzzle() const 
{ 
std::cout<< "+---+---+---+" <<std::endl; 
for(int i=0; i<3; i++) 
{ 
    for(int j=0; j<3; j++) 
    { 
     if(puzzleGame[i][j]==0) 
      std::cout<< "| "; 
     else 
      std::cout<< "| " << puzzleGame[i][j] << " "; 
    } 
    std::cout<< "|" <<std::endl; 
std::cout<< "+---+---+---+" <<std::endl; 
} 
} 
//------------------------------------------------------------------------- 

Bu görüntüler Ne her şey olur ise: Picture

cevap

0

States, Üçün Kuralını ihlal eder ve güvenli bir şekilde kopyalanamaz. What is The Rule of Three? Sorduğunuza sevindim. Bağlantıyı oku. kimse onu silindiğinde özgün ve aynı puzzleGame işaret kopya hem de States sonuçları kopyalama Bu durumda

, onunla puzzleGame alır ve diğer artık sadece Tanımsız Davranışı çağırmak için bekleyen bir yürüyüş timebomb olduğunu.

Bu bir sorundur çünkü bir sıraya basmak bir kopya oluşturur ve bu kopya kopyalanabilir, taşınabilir, silinebilir veya kuyruğun destek konteynerine ne yapması gerektiğine bakılmaksızın kopyalanabilir.

Basit düzeltme, boyutlar bilindiğinden, statik puzzleGame dizisini States dizisinin yerine statik bir şekilde yerleştirmektir. std::array may be helpful here. Daha az uygun seçenekler arasında std::vector<std::vector<States>> kullanımı ve atama gerçekleştirilmesi ve işleçleri taşıma ve puzzleGame doğru şekilde işleyen kurucular kopyala ve taşı.

+0

Ah, çok teşekkür ederim. Bu üçü doğru şekilde uygulamaya çalışırken çok fazla öfkenin ardından, harika çalışıyor. –