Passing in objects

Hi there,
I'm trying to make my project a little cleaner by making functions to do the work instead of having to repeat code, but I'm not sure how to pass the object through

Basically there is a card struct which creates the deck, and I also have a player class which creates the player and also holds the deal function
However if I want to make a function to deal the cards for me instead of hardcoding the deal function for each player, I have to pass in the card object which I am having great trouble doing

Here is a simplified version

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
struct card()
{
   string Card;
   int suit;
   int rank;
};

class player : public Card()
{
   public:
      string Card1;

      void deal();
      //my attempt = void deal(const Card& deck[]);
};

void player::deal()
{
//here i need to access deck[0] somehow
}

/*my attempt
void player::deal(const Card& deck[])
{
   card1 = deck[0].Card1;
}*/

int main()
{
   Card deck[52];
   player Player;

   Player.deal()
   //my attempt = Player.deal(deck[52]);

   return 0;
}


So as you can see the basic layout is what I need to do (access deck[0] from player::deal to define card1), and in the comments is what I have tried with no success.
Last edited on
 
class player : public Card()


means you consider a Player to be a type of Card. Is that right?

Normally a Card is a rectangular piece of cardboard and a Player is a mammal of the species Homo Sapiens.
So they look quite different and do different things.

So I would make them separate - the point of classes is to model the real world.

'deal' is something a Player does and he will need a deck of cards to do it (pass it in). he will also need
a list of other Players to deal the cards to (pass that in) the list may or may not have the dealer in it (doesn't matter). When the deal occurs the players will be receiving the cards. So player will need a 'receiveCard' method. The Deck could have a 'popTopCard' method that removes the top card and returns it.
Then you have something like

1
2
3
4
5
6
7
void Player::deal(Deck& deck,vector<Player>& players)
{
   for(int i=0;i<players.size();++i)
   {
       players[i].receiveCard(deck.popTopCard());
   }
}


this code modifies both the deck (cards will be removed) and the players (they get dealt cards which they will store). Makes sense?
Youre right about the parenting, I tried to stick the deal function in as part of the card struct as a shortcut to see if it would fix my problem. Ended up reversing the changes but didn't unparent the card struct! :P


I'm not having trouble with popping the cards or getting the players to receive the cards, my issue is passing in the deck object to the function

My error is "card is not a type"
on this line inside the class: void Deal(Card& aDeck);

However the Card struct is declared before the Deal function.
I have a strong feeling I'm making an extremely simple mistake but I just have no idea what to do


This is the code I have at the moment:

1
2
3
        Matt.cardIndex = deck[0].s_Index;
	Matt.Card1 = deck[deck[0].s_Index].Card;
	deck[0].popCard();


But I'd like to have it in a function like

1
2
3
       cardIndex = deck[0].s_Index;
	Card1 = deck[deck[0].s_Index].Card;
	deck[0].popCard();


so I can call Matt.Deal() but also Paul.Deal() instead of having to write the same code again
The main thing wrong with what you have is that deal() expects an array of cards and you give it one card. The card you are giving it also happens to be out of range (an array of size 52 can only be accessed by 0-51).

Another thing is that you've named the struct "card", but try to make "Card". Classes/Structs should begin with capitals, variables with lowercase.

when you say array[51] you are talking about the value in the array at index 51. If you say array you are talking about the address of the array in memory, and you can pass the whole thing.


line 34 could be Player.deal(deck);, and that aught to work. Or change the index (to something less than 52) at line 34, and take out the [] in the function declaration/definition.

Edit: Also, when you pass the array the function has no idea what size the array is. As such:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void player::deal(const Deck& deck[], const int SIZE)
{
  // A loop
  for (int i = 0; i < SIZE; i++)
  {
    cout << deck[i].card << endl;  
  }
}

//main

const int SIZE = 52;
Card deck[SIZE];
Player player;
player.deal(deck, SIZE);


Of course, this doesn't initialize Card's variables to anything, so it's gonna be nonsense.
Last edited on
Topic archived. No new replies allowed.