我正在为扑克游戏编写代码,但是我在找出一种从类对象传递值的方法时遇到了问题。在这种情况下,类卡...对象套牌..我想这样做来模拟交易5卡片给玩家。我创建了另一个类型为卡片的对象,名为player1 ..所以在创建了卡片之后......然后将卡片洗牌......我想从对象卡片中抽取5张卡片并将其传递给对象播放器1 ..
然而,当我打印到屏幕以查看是否传递了值时,我只得到了单元格的地址...而不是值。 这是代码
code for class deck
[代码]
class Card
{
public:
int face;
int suit;
void toString() {
cout<<FaceString[face]<<" of "<<SuitString[suit]<<endl;
}
};
[/代码]
code for class of deal
[代码]
class deal
{
public:
Card deck[52];
void toDisplay(){
for (int j = 0; j < 5; j++) {
cout<<"player card "; deck[j].toString();
}
}
};
[\代码]
[代码]
//creation of both objects
Card deck[52];
Card player1[5];
[/代码}
in main
player1.deck; <<==[Error] request for member 'deck' in 'player1', which is of non-class type 'deal [5]'
答案 0 :(得分:4)
你的功能签名错误了:
这是你的原型
void deal(const int wDeck[][13], const char *wFace[],
const char *wSuit[]);
这是你的功能实现
void deal(const int wDeck[][13], const char *wFace[],
const char wSuit[] )
{
// blah blah
}
你错过了*
答案 1 :(得分:0)
原型与功能定义不匹配,试试这个:
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');
}
}
}
}
}