Error: Invalid conversion from 'Int' to '*Card'. Help please!

Hey peeps, basically I'm trying to create a program that creates a deck, shuffles it, and deals hands. I have created the deck. Now I am trying to create an array to store the shuffled card, but I get Error: Invalid conversion from 'Int' to '*Card'. Could someone please tell me what i'm going wrong please. The rest of my class are having the same problem. Thank you.

Our assignment:
Pointers
This week you will add to last week’s assessment by writing a program that will deal two card hands.

Requirements
The requirements are that the main program must use a Card class to represent each card. You program must implement the following functions:
• createDeck – a function that creates the deck of cards
• dealCards – a function that fills up each hand of cards
• printHand(Card * hand[]) – a function that prints the hands contents and value to the screen

The main program will maintain an array variable of type Card that will represent the deck of cards. It will also maintain two arrays of type Card * (pointer to a Card) that will represent each hand. Each hand will be capable of holding five cards.

Your program will use the functions given above to create the deck and then deal the two hands with their cards. The dealing should be as random as possible so that a shuffle has been simulated. Then your program will print the contents and value of each hand.


Card.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
#include <iostream>
#include <string>
using namespace std;
//creates and holds the card suit and number.
class Card
{
    private:
        string Suit;
        int Num;

    public:
        Card()
        {
            Num = 0;
        }
        Card(int n, string s)
        {
           Suit = s;
           Num = n;
        }
        //returns the card number when called
        int getNum()
        {
            return Num;
        }
        //returns the suit type when called
        string getSuit()
        {
            return Suit;
        }
        string displayValue()
        {
            string v = "";
            switch(getNum())
            {
                case 1:
                v = "Ace"; break;
                case 2:
                v = "2"; break;
                case 3:
                v = "3"; break;
                case 4:
                v = "4"; break;
                case 5:
                v = "5"; break;
                case 6:
                v = "6"; break;
                case 7:
                v = "7"; break;
                case 8:
                v = "8"; break;
                case 9:
                v = "9"; break;
                case 10:
                v = "10"; break;
                case 11:
                v = "Jack"; break;
                case 12:
                v = "Queen"; break;
                case 13:
                v = "King";
            }
            return v + " of " + getSuit();
        }
};


main.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
#include <iostream>
#include <string>
#include "class.cpp"
#include <ctime>
#include <cstdlib>
using namespace std;
Card*hand1[5];
Card*hand2[5];
Card card[52];
Card*hand[10];
int x=0,shuffle;
void createDeck()
{
    int s, n;
    string Suit;
    //creates an array of 52 cards holding suit and number in the class
    cout<<"Deck of Cards\n";
    for (s=0;s<4;s++)
    {
        for(n=1;n<14;n++)
        {
            switch (s)
            //sets the switch values to a string, saved to string Suit
            {
                case 0: Suit= "Hearts"; break;
                case 1: Suit= "Clubs"; break;
                case 2: Suit= "Diamonds"; break;
                case 3: Suit= "Spades"; break;
            }
            card[x]= Card(n, Suit);
            //this saves each card to the array
            x++;
        }

    }
}

void dealCards()
{
    srand(time(NULL));
    int a;
    for (a=0;a<10;a++)
    {
        shuffle=rand()%52;
        hand[a]=shuffle; //This is the problem, this is obviously not the right       
                         //way to do it.
    }

}

void printHand(Card*hand[10])
{

}



int main()
{
    createDeck();
    dealCards();
    return 0;
}


Last edited on
hand, hand1 & hand2 are arrays of pointers. With that in mind, in main.cpp, on line 44, you're attempting to assign an integer to a pointer. You have to dereference a pointer to work with the pointed-to object. Otherwise, you'll be working with the pointer itself.

Wazzak
I think I understand what you mean, but the tutorial on this website doesn't help me with the this situation, or at least I can't see where it says. Could you explain a little more please, I'm not getting what I need to do at this point at all :(
Basically, pointers are not int. If the pointer is pointing to an int, you have to dereference to use it. All a pointer does is point at a place in memory that may hold something of importance.

http://www.cplusplus.com/articles/EN3hAqkS/

I hand out this link of Moschops all the time, because I think it's great
Well I now understand pointers a lot better after reading that. But I still don't know what to do in this situation. As in the examples I see, the pointer is not the array. Putting a * or a & in front on hand on line 45 just brings up a different error "no match for 'operator=' in '* hand[a] = shuffle'"

I someone could just show me how to get it to work, then I'm sure I'd be able work out what you had done and meant :) Thanks for the help so far, sorry I'm not getting it ha
Can anyone help me at all? Spoken to a couple of my class mates and they can't get it either :S ty
hand is an array of Cards, not ints. The type on the left-hand side of the assignment operator isn't the same as the type on the right-hand side. As a result, the compiler doesn't know how to perform the conversion, so it has no choice but to abort compilation.

Wazzak
Last edited on
Topic archived. No new replies allowed.