c ++混淆状态变量行为

时间:2018-04-09 17:15:35

标签: c++ arrays oop c++11

我有一个类“CardT”,它包含一个由枚举值存储的套装和编号。对于纸牌的使用,我创造了这些卡中的52个牌组。我还有一个“画面”类,为纸牌画面存储了一组这些卡片,我的棋盘类包含8个这样的阵列。将值放入数组并打印以检查是否正确后,它会给出正确的输出。但是,如果我在打印完相同的东西之后立即调用另一个函数,我会得到非常不同的值。

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

}

董事会的标题

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

最后这是给出的输出

TempVars("EmployeeType") = rs!EmployeeTypeID.Value

打印值存储为枚举。

在print语句之间明显改变,c ++非常新,但在其他方面有经验。我很想忘记任何帮助。

另外即时打印第4堆,我相信0-3堆的所有打印都正确,只有4 +正在搞乱。

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

2 个答案:

答案 0 :(得分:1)

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

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

所以你只是在那时阅读内存中遗留下来的东西,这意味着你得到了未定义的行为。

答案 1 :(得分:1)

shuffle之后的第二个循环:为什么你将我乘以8而只有6张牌?显然没有数组边界。应该是[j+4+i*6]。顺便说一下,前4个堆只有7个卡长,而i乘以8是错误的。在那些表启动循环中,j应分别小于7和6而不是8和7。