Skip to main content
 首页 » 编程设计

c++之从队列中显示二维数组后程序崩溃

2024年05月22日8shanyou

我的 C++ 程序由一个名为 States 的类组成,它必须保存我的拼图的“状态”,它是一个 3x3 int 数组和其他两个整数。当我在 main 中声明它们并打印它们时,它们使用正确的值正确打印,但是将它们放入priority_queue中,按类中的整数之一排序并将其拉回后,它在数组中有垃圾并在以下情况下崩溃:它尝试显示。我不知道为什么当我把它放进去然后拉出来时它会变得垃圾,任何人都可以解释/帮助吗?

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; 
} 
} 
//------------------------------------------------------------------------- 

这是每次显示时都会发生的情况: Picture

请您参考如下方法:

States违反了三法则,无法安全复制。 What is The Rule of Three?很高兴你问了。阅读链接。

在本例中,复制 States结果原始文件和拷贝都指向相同的 puzzleGame删除一个时需要 puzzleGame有了它,另一个现在是一个行走的定时炸弹,只是等待调用未定义的行为。

这是一个问题,因为推送到队列会生成一个拷贝,并且该拷贝可以被复制、移动、删除或队列的后备容器决定对其执行的任何其他操作。

简单的修复方法是替换动态 puzzleGame数组位于 States由于尺寸已知,因此使用静态的。 std::array may be helpful here.不太合适的选项包括使用 std::vector<std::vector<States>>并实现正确处理 puzzleGame 的赋值和移动运算符以及复制和移动构造函数.