Hi, I decided to made an account for some c++ help.
I have made a vector array of objects which represents a deck of 52 playing cards. Every new card I declared is being added to the deck. However, when I cout the amount of cards in the deck it prints the number 12. What am I doing wrong here?
CardDeck class:
1 2 3 4 5 6 7 8 9 10 11
h2 = new Card( "Deuce of hearts" );
s2 = new Card( "Deuce of spades" );
c2 = new Card( "Deuce of cloves" );
d2 = new Card( "Deuce of diamonds" );
etc...
void CardDeck::AddToDeck( Card * card)
{
deck.push_back(card);
}
Card class:
1 2 3 4 5 6
Card::Card(string _id)
{
id = _id; //sets the name of the card
deck.AddToDeck(this); //adds it to the deck array
}
And my cout:
cout << "Amount of cards in the deck: " << sizeof(deck) << endl;
sizeof operator does not show amount of elements in array, it shows size of outermost class .
For now it shows only that you have 32bit compiler. Use member function size instead: cout << "Amount of cards in the deck: " << deck.size() << endl;
#include "Card.hpp"
#include "CardDeck.hpp"
#include <iostream>
usingnamespace std;
Card::Card(string _id)
{
id = _id; //sets the name of the card
deck->AddToDeck(this); //adds it to the deck array
}
Card::~Card()
{
//dtor
}
string Card::getName()
{
return id;
}
...
void CardDeck::Start()
{
cout << "Starting.." << endl;
/*
if (state != NewCardDeck)
return;
*/
deck.clear();
h2 = new Card( "Deuce of hearts" );
s2 = new Card( "Deuce of spades" );
c2 = new Card( "Deuce of cloves" );
d2 = new Card( "Deuce of diamonds" );
h3 = new Card( "Three of hearts" );
s3 = new Card( "Three of spades" );
c3 = new Card( "Three of cloves" );
d3 = new Card( "Three of diamonds" );
h4 = new Card( "Four of hearts" );
s4 = new Card( "Four of spades" );
c4 = new Card( "Four of cloves" );
d4 = new Card( "Four of diamonds" );
h5 = new Card( "Five of hearts" );
s5 = new Card( "Five of spades" );
c5 = new Card( "Five of cloves" );
d5 = new Card( "Five of diamonds" );
h6 = new Card( "Six of hearts" );
s6 = new Card( "Six of spades" );
c6 = new Card( "Six of cloves" );
d6 = new Card( "Six of diamonds" );
h7 = new Card( "Seven of hearts" );
s7 = new Card( "Seven of spades" );
c7 = new Card( "Seven of cloves" );
d7 = new Card( "Seven of diamonds" );
h8 = new Card( "Eight of hearts" );
s8 = new Card( "Eight of spades" );
c8 = new Card( "Eight of cloves" );
d8 = new Card( "Eight of diamonds" );
h9 = new Card( "Nine of hearts" );
s9 = new Card( "Nine of spades" );
c9 = new Card( "Nine of cloves" );
d9 = new Card( "Nine of diamonds" );
h10 = new Card( "Ten of hearts" );
s10 = new Card( "Ten of spades" );
c10 = new Card( "Ten of cloves" );
d10 = new Card( "Ten of diamonds" );
hJ = new Card( "Jack of hearts" );
sJ = new Card( "Jack of spades" );
cJ = new Card( "Jack of cloves" );
dJ = new Card( "Jack of diamonds" );
hQ = new Card( "Queen of hearts" );
sQ = new Card( "Queen of spades" );
cQ = new Card( "Queen of cloves" );
dQ = new Card( "Queen of diamonds" );
hK = new Card( "King of hearts" );
sK = new Card( "King of spades" );
cK = new Card( "King of cloves" );
dK = new Card( "King of diamonds" );
hA = new Card( "Ace of hearts" );
sA = new Card( "Ace of spades" );
cA = new Card( "Ace of cloves" );
dA = new Card( "Ace of diamonds" );
}
CardDeck::CardDeck()
{
counter = 0;
}
...
void CardDeck::AddToDeck( Card * card)
{
deck.push_back(card);
counter++;
cout << counter << endl;
}
You do have its own instance of cardDeck in each card.
And it is a miracle that there is no crash in runtime.
Program architecture is cause of all of your problems. You got circular dependency in your classes which is bad. Just think: why should card know which deck contains it?