C++ 混淆了状态变量行为

c++ confusing state variable behaviour

本文关键字:变量 状态 C++      更新时间:2023-10-16

我有一个类"CardT",其中包含由枚举值存储的花色和数字。为了使用纸牌,我创建了一副包含 52 张这些牌的牌,每个牌值。我还有一个"tableau"类,用于存储一组用于纸牌的卡片,我的棋盘类包含其中的 8 张。将值放入数组并打印以检查正确性后,它会给出正确的输出。但是,如果我在打印完全相同的东西之后立即调用另一个函数,我会得到非常不同的值。

int main(){
Board b;
b.setUp();  //setUp deck and add to tableau arrays
b.printTab(4); //print the fourth one again
}

卡组的设置位置

void Board::setUp(){
//here i setup all 52
CardT deck[52];
int c = 0;
for (int i = 0; i <= 3; i++)
for (int j = 1; j <=13;j++){
deck[c] = CardT(static_cast<CardSuit>(i),static_cast<CardNum>(j));
c++;
}
//shuffle
std::random_shuffle(&deck[0],&deck[52]);
CardT tabls[8][13]; 
for (int i = 0; i < 4; i++)
for (int j = 0; j < 8; j++)
tabls[i][j] = deck[j + i*8];  //the first 4 piles that contain 8 cards
for (int i = 4; i < 8; i++)
for (int j = 0; j < 7;j++)
tabls[i][j] = deck[j + i*8]; //last four with seven
for (int i = 0; i < 4; i++)
this->tableauPiles[i] = TableauPileT(tabls[i],8); //place first four, (second param is size)
for (int i = 4; i < 8; i++)
this->tableauPiles[i] = TableauPileT(tabls[i],7); //place second four
for (int i = 0; i < 4;i++)
this->foundationPiles[i] = FoundationPileT(); //just intialize

//FOR testing
for (int j = 0; j < 13; j++){
if (this->tableauPiles[4].cardAtIndex(j)){
std::cout << this->tableauPiles[4].getCardByIndex(j).getNum() << ",,,," << this->tableauPiles[4].getCardByIndex(j).getSuit() << std::endl;
}
}
//printed twice for assurance, both print as expected
for (int j = 0; j < 13; j++){
if (this->tableauPiles[4].cardAtIndex(j)){
std::cout << this->tableauPiles[4].getCardByIndex(j).getNum() << ",,-,," << this->tableauPiles[4].getCardByIndex(j).getSuit() << std::endl;
}
}

}

在这里,我再次打印完全相同的内容:

void Board::printTab(int i){
for (int j = 0; j < 13; j++){
if (this->tableauPiles[4].cardAtIndex(j)){
std::cout << this->tableauPiles[4].getCardByIndex(j).getNum() << ",,Third,," << this->tableauPiles[4].getCardByIndex(j).getSuit() << std::endl;
}
}

}

电路板的标题

#ifndef BOARD_H
#define BOARD_H
class Board{
private:
CardT freeCells[4];
bool freeCellsOpen[4] = {false,false,false,false};
FoundationPileT foundationPiles[4];
TableauPileT tableauPiles[8];
int cardPosition(CardNum n, CardSuit s);
int emptyFreeCell();
public:
Board();
void setUp();
void moveToFreeCell(CardNum n, CardSuit s);
void moveToTableau(CardNum n, CardSuit s, int tableau);
void moveToFoundation(CardNum n, CardSuit s);
void printBoard();
void printTab(int i);
};

#endif

最后这是给出的输出 im

1,,,,1
12,,,,2
4,,,,0
4,,,,1
4,,,,2
5,,,,3
10,,,,0
1,,-,,1
12,,-,,2
4,,-,,0
4,,-,,1
4,,-,,2
5,,-,,3
10,,-,,0
1,,Third,,1
0,,Third,,1
0,,Third,,0
32545,,Third,,1284192187
0,,Third,,10
32767,,Third,,1922833024
0,,Third,,0

打印的值存储为枚举。

打印语句之间明显更改,对 c++ 非常陌生,但有 c 经验等。任何帮助都非常感谢,因为我会离开我的脑海。

我也打印第 4 堆,我相信 0-3 堆都打印正确,只是 4+ 被搞砸了。

同样值得注意的是,大的意外值在执行之间发生变化,其余保持不变。

您的牌表 (CardT tabls[8][13];) 在Board::setUp()方法的堆栈上声明。

然后,您将指向这些对象的指针存储在 TableauPileT 对象中。 这适用于您的调试代码,因为它们仍在堆栈上, 但是在调用printTab函数时,它们已被解除分配。

所以你只是在阅读内存中剩下的任何东西,这意味着你得到了不确定的行为。

洗牌后的第二个循环:为什么你把 i 乘以 8 而一堆只有 6 张牌?显然超出了阵列范围。应该[j+4+i*6].顺便说一下,前 4 堆只有 7 张牌长,将 i 乘以 8 是错误的。在这些启动循环的表中,j 应分别小于 7 和 6,而不是 8 和 7。