Trouble with class member functions

We're doing this project with simulating a game of cards. First step, deal a deck of cards. Mission accomplished. Where I am hung up is when I want to output this deck of cards. I call my_deck.print_deck(); which I expect to work as shown below

1
2
3
4
5
6
7
8
int main()
{
   //deal a deck of cards
   //creates an array from my_cards[0] to my_cards[51]
   Deck my_deck; //this part is easy
       
   //output the cards
   my_deck.print_deck();


1
2
3
4
5
Deck::Deck ()
    {
        my_deck_size = 52;
        deal(my_deck_size);
    }


1
2
3
4
5
6
7
void Deck::print_deck()
{
   for( int i = 0; i < 52; i++ )
   {
      cout << my_cards[i] << endl;
   }
}


But the compiler errors on "my_cards not declared in this scope".

I don't see what I am missing. Can anyone help?
Thanks.
What's the definition of your Deck class? Does Deck have a member variable named 'my_cards' ?
Last edited on
Thanks. That's where I am confused. Hopefully I can explain this with some intelligence:

There are two classes: Deck and Card

The function "deal" is basically this:

1
2
3
4
5
6
7
void Deck::deal()
{
   card my_cards[52];
   for (int i = 0; i<52; i++)
   {
      //initializes each card based on my_cards[i]
   }


There is a private variable in the Deck class like this:

std::list<Card> my_deck;

(I'm trying strip out the unnecessary code to keep this as simple as I can.)

So, after the constructor is called, there is an object consisting that consists of a deck of cards (actually, there can any multiple of 52 cards, but I left that part out keep this streamlined). I just need to cout the deck of cards, but I don't know how.

Am I making sense? Thanks.
Last edited on
The my_cards variable in your deal function is a local variable.
It only  exists  inside the deal  function.  When deal  returns,
my_cards is  destroyed. You should use the  private variable
my_deck in both your deal and print_deck functions, instead.

Using a std::list of Cards is probably not the best choice, as it does
not  support  random  access ( operator [] ). A std::vector  of Cards
would  be a  better choice. Bearing  in mind that a deck will  always
consist  of 52 Cards, a plain array  would be an even  better choice.
Last edited on
I see what you are saying. The basic thing that I am struggling with is how to use classes in coordination with one another. In this case, my Card class creates a single deck of 52 different cards (ace of spades, three of diamonds, etc.) My Deck class (so far) creates a deck of cards from the Card class that is based on a multiple of 52 cards, so it could be 104 cards, or 156 cards, etc. Once I get the deck established, I then need to add functionality to shuffle it, etc., but that's the next challenge.

For now, Deck has two local variables: my_deck_size (integer of total cards) and my_deck. That's the tricky one. Its type is Card, and is declared like this:

std::list<Card> my_deck;

In my constructor, after I set my_deck_size to 52 or whatever, how do I initialize my_deck? Is something like this?:

my_deck = deal(my_deck_size)

I have to use the Card class within the Deal class because it has member functions that set up each of the cards.

Sorry I'm totally confused by this, but I don't know what to do. Thanks a lot for your help.
joatmon wrote:
In my constructor, after I set my_deck_size to 52 or whatever,
how do I initialize my_deck? Is something like this?:

my_deck = deal(my_deck_size)

Well, you can't do that, since (1) deal doesn't return anything and (2) deal doesn't have any arguments.

Just call deal like this -> deal();. Note that you can access both my_deck and my_deck_size inside deal.

Is the Card class provided by your instructor? Could we see it?
The Card class is provided my my instructor, but it's too long to post. What you probably would be interested in is that it has three private variables (my_rank, my_suit, my_value), all size_t.

I am totally confused (not by what you suggested, but by what to do next). I'm just going to lay out my Deck class, as ridiculous and incomplete as it is:

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
38
39
40
41
#ifndef DECK_H
#define DECK_H

#include <iostream>
#include <cstdio>
#include <sstream>
#include <string>
#include <list>
#include "card.h"

namespace csci2270_assignment5
{
    class Deck
    {
    public:
        // constructors---------------------------------------------------------
        typedef std::size_t size_type;
        Deck ();
        Deck (size_t deck_count);
        
        // const Member Functions-----------------------------------------------
        void deal();
        void print_deck();
        
        // modifier Member Functions--------------------------------------------
        
        // friend Functions-----------------------------------------------------
        
            
    private:
        // helper functions ----------------------------------------------------
        
        // private member data--------------------------------------------------
        std::list<Card> my_deck;
        std::list<Card> my_discard_pile;
        std::size_t my_deck_size;
    };
    
}

#endif 



deck.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
#include "deck.h"
#include "card.h"

using namespace std;

namespace csci2270_assignment5
{
    // constructors---------------------------------------------------------
    // Postcondition: establishes a single deck of 52 cards
    Deck::Deck ()
    {
        my_deck_size = 52;
        deal();
    }
    
    // Postcondition: establishes a deck of cards consisting of a multiple
    //  of 52 cards * deck_count  
    Deck::Deck (size_t deck_count)
    {
        my_deck_size = deck_count * 52;
        
        deal();
    }
        
    // const Member Functions-----------------------------------------------
    void Deck::deal()
    {
        int card_index = 0;

        //Create an empty array with enough space for all the cards
        Card my_cards[my_deck_size];

        //deal the cards
        for (int j = 0; j < (my_deck_size / 52); j++)
        {
        	for(int i = 0; i < 52; i++, card_index++)
        	{
        		if( i < 13 )
        		{
        			my_cards[card_index].set_rank((i % 13) + 1);
        			my_cards[card_index].set_suit(1);
        			my_cards[card_index].set_face_up(false);
        		}
        		else if( i < 26 )
        		{
        			my_cards[card_index].set_rank((i % 13) + 1);
        			my_cards[card_index].set_suit(2);
        			my_cards[card_index].set_face_up(false);
        		}
        		else if( i < 39 )
        		{
        			my_cards[card_index].set_rank((i % 13) + 1);
        			my_cards[card_index].set_suit(3);
        			my_cards[card_index].set_face_up(false);
        		}
        		else
        		{
        			my_cards[card_index].set_rank((i % 13) + 1);
        			my_cards[card_index].set_suit(4);
        			my_cards[card_index].set_face_up(false);		
        		}
        	}
        }
    }
        
	void Deck::print_deck()
	{
        for( int i = 0; i < my_deck_size; i++ )
    	{
    		//cout << my_deck[i] << endl;
    	}
    }
    
}


This will compile, but it doesn't do what I want. The main problem is in that in deal(), I'm creating an array based on the Card class called my_cards, but what i really need to do is create one in the Deck class. I don't know how to use the functionality from Card within Deck. I'm really stuck.

Thank you for your help. I don't know where else to turn.
Last edited on
Well, as I said above, this -> Card my_cards[my_deck_size]; shouldn't be inside the deal function.

It should be here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Deck
{

    //...

private:

    //...

    //std::list<Card> my_deck;     <- You don't want this.
    std::vector<Card> my_cards; // <- This is what you want. 
    
    //...
};

And deal should look like this:

1
2
3
4
5
6
7
8
9
10
11
Card Deck::deal()
{
    my_cards.resize(my_deck_size);
    
    //...

    // Nothing changes here. Use my_cards
    // just like you would use a plain array.

    //...
}
Last edited on
Thanks, but I can't do it that way. We are not allowed to use vectors, and I have to store the cards in the Deck private variable my_deck.

Is there anything that you can tell me that will help me accomplish it this way? Maybe the whole idea of the deal() function is the wrong approach? Somehow, I have to wind up with the my_deck filled with the various cards.
You mean my_deck has to be a std::list? This is very strange. How are you supposed to
implement a random shuffle algorithm on a container that doesn't support random access?

Anyway, in this case, you can use your temporary my_cards array,
and once you fill it up, copy it to my_deck, like this (pseudocode):

1
2
3
4
for (each element in my_cards)
{
    my_deck.push_back(current element);
}

Also, note that if you use a list, you'll have to use iterators in your
print_deck function -> http://cplusplus.com/reference/stl/list/begin/
Thank you. However, now I can't even get my_deck set up. I'm going to address that in another post. I really appreciate your help.
This was a good idea. I have everything set up, but now my output doesn't work. I just get an empty screen. I'll put it in a different post since it's a little different problem.

Thanks again!
Topic archived. No new replies allowed.