hello there i have been stuck for 2 hours on trying to pass an array from private class to another:
here is my 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 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
|
class deck {
private:
card cards[52];
int num_cards;
public:
void create_cards(card *cards,char *zat);
void print();
void shuffle(card *cards);
};
class game {
private:
deck cards;
player *players;
dealer game_dealer;
int num_players;
public:
void n_players(int num_players);
void create(card *cards);
void shuffle(card *cards);
void get_card2(card *cards);
void print(card *cards);
};
int main(){
card cards[52];
char *z;
char s;
int n;
game g;
g.n_players(n);
g.create(cards);
g.shuffle(cards);
g.print(cards);
g.get_card2(cards);
}
void deck::shuffle(card *cards){
for(int i=0; i<52; i++){
int j=(rand()+time(0)) % 52;
int temp = cards[i].value;
cards[i].value=cards[j].value;
cards[j].value= temp;
char temp1 = cards[i].suit;
cards[i].suit=cards[j].suit;
cards[j].suit= temp1;
}
}
void game::shuffle(card *cards){
cards.shuffle(cards);
}
};
|
the error i get is:
t.cpp:199:10: error: request for member ‘shuffle’ in ‘cards’, which is of pointer type ‘card*’ (maybe you meant to use ‘->’ ?)
cards.shuffle(cards);
Last edited on
Since cards
is a pointer you need to access it either with
cards->shuffle(cards);
or
cards[...].shuffle(cards);
But I don't think that you want either. Rather make shuffle(...) a free or static function.
Does cards
have a function cards::shuffle(...)
?
I think you wanted to use deck::shuffle(...)
, or not?
Why don't you just make everything public:
. Would this affect your code in another way?
Why don't you just make everything public: . Would this affect your code in another way? |
I think someone needs a little refresher on the principles of OOP...
Last edited on