-
problem using pointers
hi there i seem to have a problem setting my array of cards to null, well i think thats the problem
Code:
class deck {
public:
deck(); //Constructor
void createDeck();//Fills array with legal cards
void shuffle(); //Shuffles those cards
card drawCard(); //Gets a card from the deck
card* cards[52];
};
deck::deck() {
createDeck(); //Set up the deck
}
how would i set the array of cards to null?
maybe if i tell that when i run my program it crashes straight away but it does compile i can get more info if you need it but i wouldnt know what you need
thanks in advance :)
Code:
class card
{
public:
int value;
char suit;
};
this is my card class
my problem lies in that im trying to test my code by "cout<<" the deck out into a consol but it doesnt work it just crashes
-
> card* cards[52];
This most likely should be:
card cards[52];
You could also use a vector:
Code:
vector<card> cards;
Then adding a card is easy:
Code:
card Card = {10, 'd'};
cards.push_back(Card);
-
ah ha super that now displays a load of junk in the consol but it dont crash so its a start for me to work on :)
thanks for the advice :)
im gonna try to use a vector for the hands in my game as ive never used one before
so hopefully this project is gonna be fun
-
Your card class could have a constructor that sets the value and suit to some value meaning "unspecified". That way you won't get junk.
Of course, your deck constructor should probably initialize the cards to valid values anyway and that would fix the problem as well. You have a createDeck function but good design normally has the object initialized properly in the constructor (in this case that means creating the deck).