I am new to C++ and trying to create some kind of small casino for a school project, I have the following classes: Game.h, Deck.h, Card.h, Player.h
I have a function that populates the deck, which calls another function called "AddCard" which is within Deck.h.
my problem is I created an array of cards in Deck class and it has the size of 52. when im running in a for loop to populate the deck its succesfuly adds the first item and than when trying to add deck[1] it says stackoverflow.
my code is the following: Deck constructor:
const int maxSize = 52;
Deck:: Deck() {
amountOfCards = 0;
deck = new Card[maxSize];
}
addCard function where it fails:
void Deck::addCard(Card & c) {
if (!isFull())
{
deck[amountOfCards] = c;
amountOfCards++;
}
}
My Initialize deck code:
Card* C;
//Initialize Deck
for (int i = 0; i < 4; i++)
{
for (int j = 2; j < 11; j++)
{
C = new Card(j, Card::symbol(i), _T("Test"));
this->mainDeck.addCard(*C);
}
Card *jack = new Card(11, Card::symbol(i), _T("JACK"));
this->mainDeck.addCard(*jack);
Card* queen = new Card(12, Card::symbol(i), _T("QUEEN"));
this->mainDeck.addCard(*queen);
Card* king = new Card(13, Card::symbol(i), _T("KING"));
this->mainDeck.addCard(*king);
Card* ace = new Card(14, Card::symbol(i), _T("ACE"));
this->mainDeck.addCard(*ace);
}
and the exception is: Exception thrown at 0x78A40BBA (mfc140ud.dll) in FinalProject.exe: 0xC0000005: Access violation reading location 0xFDFDFDF9.
I would love to get some help, thanks!
Edit:
IsFull function:
bool Deck::isFull() {
if (amountOfCards == 52)
return true;
return false;
}
The Initialize deck happens in Game.cpp constructor:
Game::Game(Player& p1, Player& p2) {
this->players[0] = p1;
this->players[1] = p2;
this->turn = 0;
this->isGameOver = false;
this->mainDeck = new Deck();
Card* C;
//Initialize Deck
for (int i = 0; i < 4; i++)
{
for (int j = 2; j < 11; j++)
{
C = new Card(j, Card::symbol(i), _T("Test"));
this->mainDeck.addCard(*C);
}
Card *jack = new Card(11, Card::symbol(i), _T("JACK"));
this->mainDeck.addCard(*jack);
Card* queen = new Card(12, Card::symbol(i), _T("QUEEN"));
this->mainDeck.addCard(*queen);
Card* king = new Card(13, Card::symbol(i), _T("KING"));
this->mainDeck.addCard(*king);
Card* ace = new Card(14, Card::symbol(i), _T("ACE"));
this->mainDeck.addCard(*ace);
}
}
Deck class variables
class Deck:public Card
{
private:
int amountOfCards;
Card* deck;
I think my Card array isnt initalized right.. like its not getting allocated .. its only able to add 1 item to this array and than crash.
User contributions licensed under CC BY-SA 3.0