Evan
Evan

Reputation: 1

Why am I receiving "Expression must have a class type" error? (C++)

wDeck, in the code below is giving me an "expression must have a class type" error. I've already had wDeck clarified in most of the other code void fillDeck(Card* wDeck, void shuffle(Card* wDeck), void deal( Card wDeck[][13]))

I don't understand it

I haven't tried much since I only have basic knowledge of C++

I use Visual Studio 2019, Enterprise

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
using namespace std;

struct Card {
    char *face;
    char *suit;
};

void shuffle(int[][13]);
void deal(const int[][13], const char* [], const char* []);
void fillDeck(Card*, char* [], char* []);


int main() {
    const char* suit[4] =
    { "Hearts", "Diamonds", "Clubs", "Spades" };
    const char* face[13] =
    {
        "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
    int deck[4][13] = { 0 };

    srand(time(0));

    shuffle(deck);
    deal(deck, face, suit);

    return 0;
}

void fillDeck(Card* wDeck, char* wFace[], char* wSuit[])
{
    for (int i = 0; i < 52; i++) {
        wDeck[i].face = wFace[i % 13];
        wDeck[i].suit = wSuit[i / 13];
    }
}

void shuffle(Card* wDeck)
{
    for (int i = 0; i < 52; i++) {
        int j = rand() % 52;
        Card temp = wDeck[i];
        wDeck[i] = wDeck[j];
        wDeck[j] = temp;
    }
}

void deal( Card wDeck[][13])
    {
        for (int i = 0; i < 52; i++)
            cout << setiosflags(ios::right)
            <<setw(5) << wDeck[ i ].face << " of "
            <<setiosflags(ios::left)
            <<setw(8) << wDeck[ i ].suit
            <<( ( i + 1 ) % 2 ? '\t': '\n' );
    }```

Upvotes: 0

Views: 172

Answers (1)

ma1169
ma1169

Reputation: 1158

In this code

    void deal( Card wDeck[][13])
    {
        for (int i = 0; i < 52; i++)
            cout << setiosflags(ios::right)
            <<setw(5) << wDeck[ i ].face << " of "
            <<setiosflags(ios::left)
            <<setw(8) << wDeck[ i ].suit
            <<( ( i + 1 ) % 2 ? '\t': '\n' );
    }

you defined wDeck[][13] as a multi-dimensional array then you tried to access it as a 1 dimensional array

 wDeck[ i ]

what this does is the following:

In the first iteration the result of wDeck[ 0 ] is another array, in your case it has 13 elements wDeck[i] is not a class or a struct, it is an array hence it does not have member functions.

it was answered here in your code, you can substitute arrayTest in here with wDeck[i] in your code, then you can see how you both did the same mistake, so now you are trying to access an array of 13 element using struct member pointer. hence you get the same error

what you need is use nested loop

for (int i = 0; i < 4; i++)
{
 for (int j =0 ; j<13 ;j++)
{
 cout << setiosflags(ios::right)
            <<setw(5) << wDeck[ i ][j].face << " of "
            <<setiosflags(ios::left)
            <<setw(8) << wDeck[ i ][j].suit
            <<( ( i + 1 ) % 2 ? '\t': '\n' );
}
}

What this code does is, it iterates over the first layer of the array wDeck[](i think you defined this array as 4 after reading your code), the result will be an array of the second layer(i can see you defined it as 13), then you iterate over the second layer of the array and then you get an object of type Card where you can use it's members and functions if it has any

I think you need to learn more about multi-dimensional array and nested loop possibly

excuse my English

Upvotes: 1

Related Questions