passing value by reference but still showing addre

2019-09-21 09:23发布

问题:

I am writing code for a poker game, but I am having problems figuring out a way to pass value from a class object..in this case class card..object deck.. I want to do this to simulate dealing 5 cards to a player..I created another object of type card called player1.. so after creating the deck ..and then shuffling the deck..i want to draw 5 cards from the object deck..and pass it to the object player1..

However when I print to screen to see if value is passed in i only get the adress of cell..not the value. Here is the code

code for class deck

[code]

 class Card
        {
        public:
            int face;
            int suit;

            void toString() {
                cout<<FaceString[face]<<" of "<<SuitString[suit]<<endl;
            }

        };

[/code]

code for class of deal

[code]

 class deal
        {
        public:
        Card deck[52];

            void toDisplay(){
                for (int j = 0; j < 5; j++) {  
                cout<<"player card "; deck[j].toString();
                 }
            }   
        }; 

[\code]

[code]

  //creation of both objects
            Card deck[52];
             Card player1[5];

[/code}

in main


player1.deck; <<==[Error] request for member 'deck' in 'player1', which is of non-class type 'deal [5]'

回答1:

You got wrong function signature:

This is your prototype

void deal(const int wDeck[][13], const char *wFace[],
            const char *wSuit[]);

This is your function implementatiion

    void deal(const int wDeck[][13], const char *wFace[],
               const char wSuit[]  )
{
     // blah blah
}

you miss a *



回答2:

The prototypes did not match the function definitions, try this:

void shuffle(int wDeck[][13]);
void deal(const int wDeck[][13], const char *wFace[], const char* wSuit[]);

void shuffle(int wDeck[][13])
{
    int row;
    int column;
    int card;

    for (card = 1; card <= 52; card++){
        do{
            row = rand() % 4;
            column = rand() % 13;
        } while (wDeck[row][column] != 0);

        wDeck[row][column] = card;
    }
}

void deal(const int wDeck[][13], const char *wFace[], const char* wSuit[])
{
    int card;
    int row;
    int column;

    for (card = 1; card <= 52; card++){

        for (row = 0; row <= 3; row++){

            for (column = 0; column <= 12; column++){
                if (wDeck[row][column] == card){
                    printf("%5s of %-8s%c", wFace[column], wSuit[row],
                        card % 2 == 0 ? '\n' : '\t');

                }
            }
        }

    }
}