Problem in returning from an Array

Hello all,

I' m new to C++ and my problem might be easy. But can you please help me fixing it?

I have two classes: Deck.cpp and Card.cpp and the two header files: Deck.h and Card.h ofcourse.

Each card has a Suit, and a Value.. which are both Enums. These cards are kept in an Array, which is called the deck. Now in the constructor of the Deck, I fill the Array with all the 52 cards. When I want to return the first card of the Deck, by calling the method getNextCard(), the methods getSuit() and getValue() of the card return a complete wrong number!

The first card should have Suit: 0 (Because Diamonds is 0 in the Suit Enum) and Value: 0 (Because Two is 0 in the Value Enum). In stead of that it returns:

4198464 0


And when I want to return the fifth card of the array, it still returns something different:

419673 0


Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef DECK_H
#define	DECK_H

#include <iostream>
#include <list>
#include "Card.h"

using namespace std;

class Deck {
public:
    Deck();
    Deck(const Deck& orig);
    virtual ~Deck();
    void reset();
    Card getNextCard();
private:
    Card deck[];
    int nextCard;
};

#endif	/* DECK_H */ 


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
#include <stdlib.h>

#include "Deck.h"

Deck::Deck() {
    nextCard = 0;

    int i = 0;
    for (Card::Suit suit = Card::DIAMONDS; suit <= Card::CLUBS; suit = Card::Suit(suit + 1)) {
        for(Card::Value value = Card::TWO; value <= Card::ACE; value = Card::Value(value + 1)){
            Card card = Card(suit, value);
            deck[i] = card;
            i++;
        }
    }
}

Deck::Deck(const Deck& orig) {
}

Deck::~Deck() {
}

void Deck::reset(){
    int i = 0;
    for (Card::Suit suit = Card::DIAMONDS; suit <= Card::CLUBS; suit = Card::Suit(suit + 1)) {
        for(Card::Value value = Card::TWO; value <= Card::ACE; value = Card::Value(value + 1)){
            Card card = Card(suit, value);
            deck[i] = card;
            i++;
        }
    }
}

Card Deck::getNextCard(){
    return deck[nextCard++];
}


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
#ifndef CARD_H
#define	CARD_H

class Card {
public:
    enum Suit{
        DIAMONDS,
        SPADES,
        HEARTS,
        CLUBS
    };
    enum Value{
        TWO,
        THREE,
        FOUR,
        FIVE,
        SIX,
        SEVEN,
        EIGHT,
        NINE,
        TEN,
        JACK,
        QUEEN,
        KING,
        ACE
    };
    Card(Suit s, Value v);
    Card(const Card& orig);
    virtual ~Card();
    Suit getSuit();
    Value getValue();
private:
    Suit suit;
    Value value;
};

#endif	/* CARD_H */ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "Card.h"

Card::Card(Suit s, Value v) {
    suit = s;
    value = v;
}

Card::Card(const Card& orig) {
}

Card::~Card() {
}

Card::Suit Card::getSuit(){
    return suit;
}

Card::Value Card::getValue(){
    return value;
}
This is likely due to your implementation of the Card copy instructor - it doesn't initialize any members.
Note that you generally should use the initializer list for initializing members instead of using assignments in the constructor body, i.e.:
Card::Card(const Card& orig) : suit(orig.suit), value(orig.value) {}

The same can be achieved by simply removing the copy constructor - in that case, the compiler will provide a default copy constructor, which does the same as the one above.
Thanks a lot for your reply!

You were right it was indeed the implementation of the Card copy instructor.
Topic archived. No new replies allowed.