Advertisement

Pointer Help

Started by October 08, 2000 12:05 PM
2 comments, last by Peddler 24 years, 2 months ago
Here''s my problem...I have a small program which so far creates a shuffled pile of cards and I am now trying to get them to be dealt to a Hand one by one....but....It seems that when I deal the Card object to the Hand object, I am not passing that Card''s values but the address so every card in the Hand points to only 1 actual Card thus they are all the same. Here is a little breakdown of the classes: The Card class is made up of a few simple members, rank and suit. The Pile class is made up of an array of Card objects: Card *Cards; The Pile is initialized fine and shuffled correctly. It has a Deal method which will return a Card, this method looks like this:
    
Card Pile::Deal()
{
	m_NumOfCards--;
	return (Cards[m_NumOfCards]);
}
[/source]

The Hand class is made up of a small array of Card objects and also has a Deal method which takes in a Card object and adds it to it''s own array.

[source]
class Hand
{
private:
	Card Cards[10];
public:
	Hand() { m_NumOfCards = 0; }
	
	void Deal(Card &TempCard) { 
		m_NumOfCards++;

/* Right here, it is not passing the values but the address so every card in the array will be the same...how do I just copy the values of TempCard over?
*/
		Cards[m_NumOfCards] = TempCard; 
	}

	int m_NumOfCards;
};
    
Any suggestions as to what I should try? thanks -tim
It''s not exactly clear what the problem is, it seems to work!?

What do you want it to do?
What does it do?

if you want to return the address of a Card obect from the Pile::Deal method, it should look like this

    Card* Pile::Deal()	{	m_NumOfCards--;	return (&Cards[m_NumOfCards]);	}    
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
Sorry for the lack of info...I was a little eager to get something posted....

Basically, I want to transfer one of the Cards in the Pile class to the array of Cards in the Hand class.

What is happening is that when I do this in the Hand class:

    void Deal(Card &TempCard) { 	m_NumOfCards++;	Cards[m_NumOfCards] = TempCard; }    


All the Cards in the Hand''s array all point to the last TempCard that was sent in, even though a different card is sent in everytime from the shuffled deck.

I want each card that is sent into the Deal method above that is then added to the Hand''s Card array to retain the values of that card....right now, If I deal say 5 cards to the Hand, even though every card that goes into the Deal method above are individual and different, for some reason, they all end up having the same exact suit and rank values.

I''m not sure if I am making any sense, but i just can''t seem to get this functioning...it seems like it is coded right.



Ugh..nevermind. It''s working now and was working, my display method was incorrect. It seems that its always the simplist mistakes that are the hardest to find.

This topic is closed to new replies.

Advertisement