I am fairly new to programming and am trying to create a program to play the card game crazy eights against a computer opponent. I am getting this error in my function that adds hands from the deck to the players hand. Here is my hand class:
class Hand {
private:
Card* cards;
int n_cards; // Number of cards in the hand.
public:
// must have constructors, destructor, accessor methods, and mutator methods
Hand();
~Hand();
Hand(const Hand&);
Hand& operator= (const Hand&);
void set_cards(int n, Card card);
void set_n_cards(const int n_cards);
Card get_cards(int i);
int get_n_cards();
void increase_n_cards();
bool ifPlayable(int cardNum, Card& topPile);
void isEight(Card&);
void playCard(int index);
};
and here is the function:
void Hand::set_cards(int n, Card card)
{
Card** temp = &cards;
Card* new_hand = new Card[n_cards+1];
for(int i = 0; i < n_cards; i++)
{
new_hand[i].setCard((*temp)[i].getSuit(),(*temp)[i].getRank());
}
new_hand[n_cards].setCard(card.getSuit(),card.getRank());
delete[] *temp;
*temp = nullptr;
*temp = new_hand;
n_cards++;
}
I am very confused as I haven't gotten this type of seg fault before and would love some help. Also, n_cards keeps reverting back to 0 after the function ends. Any idea why this may be happening?
Aucun commentaire:
Enregistrer un commentaire