Wrong amount of elements in my array?

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;


Thanks in advance
You need to use deck.size(), because sizeof(deck) is the size of the vector class, not how many elements it contains.
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;
Last edited on
Aah ofcourse! Silly mistake. Thanks!
Now that I used deck.size, I added a counter for every card thats added to the array. It somehow always prints "1" now :S

Can't figure out what the cause is...
Last edited on
Where and how is deck declared? Unless it's static or global, you have a separate deck inside each card.
The deck array is declared in the CardDeck class:
std::vector<Card* > deck;

What exactly do you mean by a seperate deck in each card?
You misunderstand.
1
2
3
4
5
6
Card::Card(string _id)
{
   id = _id;
   deck.AddToDeck(this); //<-- Where does this "deck" come from? Where is it?

}
I declared it in the header file:

CardDeck deck;


Or am I supposed to make it a pointer?
I declared it in the header file:

CardDeck deck;

But how? Is it a data member? A static data member? A global variable? Something else? Can we actually see your code?

Last edited on
Sorry, here is my code:

Card.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef CARD_H
#define CARD_H

#include <string>
#include <iostream>

#include "CardDeck.hpp"

class Card
{
    public:
        Card(std::string _id);
        std::string getName();

        CardDeck * deck;

        virtual ~Card();
    protected:
    private:
        std::string id;
};

#endif // CARD_H 


Card.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Card.hpp"
#include "CardDeck.hpp"
#include <iostream>

using namespace 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;
}


CardDeck.hpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef CARDDECK_H
#define CARDDECK_H

#include <vector>

class Card;
class CardDeck
{
    public:
        CardDeck();
        void Start();
        void deal();
        void update();
        void AddToDeck(Card * card);
        void console();
        int shuffle();

        virtual ~CardDeck();

    protected:

    private:

        enum GameState { Uninitialized, NewCardDeck, Dealing, Menu , Turn1, Turn2, Exiting };

        GameState state;

        int counter;

        std::vector<Card* > deck;
        //CARDS
        //deuces
        Card * h2;
        Card * s2;
        Card * c2;
        Card * d2;
        //threes
        Card * h3;
        Card * s3;
        Card * c3;
        Card * d3;
        //fours
        Card * h4;
        Card * s4;
        Card * c4;
        Card * d4;
        //fives
        Card * h5;
        Card * s5;
        Card * c5;
        Card * d5;
        //sixes
        Card * h6;
        Card * s6;
        Card * c6;
        Card * d6;
        //sevens
        Card * h7;
        Card * s7;
        Card * c7;
        Card * d7;
        //eights
        Card * h8;
        Card * s8;
        Card * c8;
        Card * d8;
        //nines
        Card * h9;
        Card * s9;
        Card * c9;
        Card * d9;
        //tens
        Card * h10;
        Card * s10;
        Card * c10;
        Card * d10;
        //jacks
        Card * hJ;
        Card * sJ;
        Card * cJ;
        Card * dJ;
        //queens
        Card * hQ;
        Card * sQ;
        Card * cQ;
        Card * dQ;
        //kings
        Card * hK;
        Card * sK;
        Card * cK;
        Card * dK;
        //aces
        Card * hA;
        Card * sA;
        Card * cA;
        Card * dA;

};

#endif // CARDDECK_H 



CardDeck.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
...

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;

}






edit: piece of code from the main class:

1
2
3
 CardDeck cards;

 cards.Start();
Last edited on
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?
Topic archived. No new replies allowed.