[英]Runtime error while generating object's values in the constructor of an object that aggregates them
我正在為模擬撲克游戲的入門課程編寫C ++程序。 我創建了一個帶有整數的卡片對象,用於存儲面部和西服的值。 然后,我創建了一個DeckOfCards對象,該對象具有一個由52個卡對象組成的C ++ 11數組。 我正在嘗試為卡片分配值,並在deckOfCards構造函數中洗牌。 經過一些測試之后,我相信我已經將此問題歸結為這張面孔和西裝的生成過程。 我還嘗試通過在dealCard()方法中返回對這些對象的引用來與其他類(手)共享這些對象,但是我認為在實例化手之前發生了錯誤。 該錯誤發生在我的Arch-Linux VM和Visual Studio的GCC中,因此它似乎是編譯器所不支持的(盡管clang甚至不會編譯它)。 甲板的構造函數:
DeckOfCards::DeckOfCards(bool startShuffled) //Constructor.
{
/************************************************************
Constructor iterates through the four suits and 13 faces,
generating a card for each combination. This adds up to a
deck of 52 unique cards. By default it shuffles the deck to
prevent accidental usage of an unshuffled deck in a card
game. Passing 'false' as an arguement prevents the initial
shuffle.
************************************************************/
int arrayLoc = 0; //The card being initialized (its a subscript of deck).
for (int i = 0; i < 4; i++) //For each of the four suits.
{
for (int j = 0; j < 13; j++) //For each face in that suit.
{
deck[arrayLoc].setFace(j); //Create a card with that face.
deck[arrayLoc].setSuit(i); //And with that suit.
arrayLoc++; //Then move to the next card.
}
}
currentCard = 0; //Sets the top of the deck.
if (startShuffled)
shuffle(); //Shuffles the deck by default.
}
VS2013的調試器說,在通過此循環的第一次迭代之后,它無法讀取deck [0]的面孔並適合數據。 其他人的行為也與此類似。 主程序本身(這里未顯示)正在運行,直到我嘗試通過指向牌組的指針數組將牌發給手牌時,我認為它與牌組生成期間的不正確初始化有關。 如果沒有人發現我的問題,我可以提供更多代碼。
卡的獲取器/設置器:
void Card::setFace(int faceVal)
{
if (faceVal >= 0 && faceVal < 13) //Validates value before setting.
face = faceVal; //Sets the value of Card's face.
else
throw invalid_argument("Face value must be between 0 and 12 (inclusive)");
}
void Card::setSuit(int suitVal)
{
if (suitVal >= 0 && suitVal < 4) //Validates value before setting.
suit = suitVal; //Sets the value of Card's suit.
else
throw invalid_argument("Suit value must be between 0 and 3 (inclusive)");
}
//Getters
int Card::getFace() const //Returns face by value.
{
return face;
}
int Card::getSuit() const //Returns suit by value.
{
return suit;
}
編輯:(添加信息)我的手類有五個指向Card對象的指針,它通過調用DeckOfCard的dealCard()方法從卡片組中檢索該指針。
const Card DeckOfCards::dealCard()
{
/*************************************************
If the deck has not depreciated, this returns a
const reference to the card at the top of the deck
and increments to the next card. If it is depreciated
it throws an exception.
*************************************************/
if (moreCards())
{
unsigned int tempCurrentCard = currentCard;
currentCard++;
return deck[tempCurrentCard];
}
else
{
throw invalid_argument("The deck is out of unused cards. Must shuffle to continue.");
}
}
Hand的構造函數:
Hand::Hand(const Card &card1, const Card &card2,
const Card &card3, const Card &card4, const Card &card5)
{
/*****************************************************
Constructor initializes a c++11 array of pointers to
card objects.
*****************************************************/
//Initialize the cards in hand. (as pointers)
cardsInHand[0] = &card1;
cardsInHand[1] = &card2;
cardsInHand[2] = &card3;
cardsInHand[3] = &card4;
cardsInHand[4] = &card5;
//Calculate the value of the hand.
calcHandScore();
}
我通過使用myDeck.dealCard()作為手的構造函數中的每個參數來實例化main。 這種數據交接似乎是可能發生錯誤的地方。 Hand player1(deck.dealCard(), deck.dealCard(), deck.dealCard(), deck.dealCard(), deck.dealCard());
但這也可能與套牌的改組算法有關:
void DeckOfCards::shuffle()
{
/***********************************************************
Shuffles the deck of cards by via a random number generator.
Should be called whenever the current card is the final
card in the deck.
***********************************************************/
srand(time(NULL)); //Creates a random seed for number generation.
int randomValue; //Stores random number for card selection.
//Loops through each card in deck and swaps it with another.
for (int i = 0; i < 52; i++) //Iterates through the deck.
{
randomValue = rand() % 51; //Selects a random card to swap.
if (randomValue != i) //Prevents self assignment.
{
//Swaps the cards values, effectively swapping their locations.
int tempFace = deck[randomValue].getFace(); //Holds a copy of selected card.
int tempSuit = deck[randomValue].getSuit();
deck[randomValue].setFace(deck[i].getFace());
deck[randomValue].setSuit(deck[i].getSuit());
deck[i].setFace(tempFace);
deck[i].setSuit(tempSuit);
}
}
currentCard = 0; //Shuffle resets the "top" of the deck.
}
這聽起來很愚蠢,但是在調試器中逐步調試代碼后,我意識到類邏輯都是合理的。 問題出在另一個for循環內部,其中有一個for循環,用於遞增錯誤的變量(即for(int j = 5; i <52; j ++) )。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.