• Forum
  • Jobs
  • When i cout << it give an address :/

 
When i cout << it give an address :/

Hi, i have an array of pointers to pointers. Its a deck of cards and we've been told in class to do it that way. I have a build_deck() function and a shuffle_deck() function. They both work fine. And now im onto the deal_card() function, and it works but it just prints out the address of what its pointing to. Any help would be greatly appreciated, here is some of the code :)

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

Deck::Deck( int total_cards )
{
    _total_playing_cards = total_cards;

    _playing_cards = new PlayingCard*[total_cards];

    _deal_next = 0;
}

void Deck::build_deck()
{

    for( int suit = 1; suit <= 4 && _deal_next != 51; suit++ && _deal_next+13)
        {
            for( int fvalue = 1; fvalue <= 13; fvalue++)
                {
                    _playing_cards[_deal_next] = new PlayingCard( fvalue, suit );
                    cout << PlayingCard( fvalue, suit ) << endl;
                }
           
        }

}

void Deck::shuffle_deck()
{

std::random_shuffle(_playing_cards, _playing_cards + 52);
     
}

PlayingCard Deck::deal_card()
{
 cout << _playing_cards[_deal_next] << endl;
}
This should be a beginner question. You just have to dereference the pointer with *.
sorry, but ive already tried that, it just causes my program to crash and close :/
That just means your pointers don't actually point to correct data.
new PlayingCard*[total_cards];
i know new PlayingCard[total_cards];
int * p1 = new int[5];
Operators new and new[]
In order to request dynamic memory we use the operator new. new is followed by a data type specifier and -if a sequence of more than one element is required- the number of these within brackets []. It returns a pointer to the beginning of the new block of memory allocated. Its form is:

pointer = new type
pointer = new type [number_of_elements]
http://www.cplusplus.com/doc/tutorial/dynamic/
Topic archived. No new replies allowed.