我试图让我的player1Hand功能与我的牌组功能一起使用,因此牌可以从牌组中抽出并用于每个牌手功能。我不确定如何从另一个函数调用一个函数,因为它说,
main.cpp:68:10: error: 'deck' was not declared in this scope
cout << deck[ z ].rank << " of " << deck[ z ].suit << endl;
完整代码:
#include <iostream>
#include <cstdlib> //for rand and srand
#include <cstdio>
#include <string>
#include <ctime> // time function for seed value
#include "Card.h"
using namespace std;
#define pause cout << endl; system("pause")
class CardClass {
public:
struct card
{
string rank;//this example uses C++ string notation
string suit;
int value;
};
public:
void deckFunction ();
private:
};
void CardClass::deckFunction () {
srand(time(0));
struct card deck[52]; // An array of cards named deck, size 52
const string ranks[ ] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King" };
const string suits[ ] = { "Diamonds", "Hearts", "Spades", "Clubs" };
int k = 0;
for ( int i = 0; i < 13; i++)
{
for ( int j = 0; j < 4; j++)
{
deck[ k ].rank = ranks[ i ];
deck[ k ].suit = suits[ j ];
k++;
}
}
}
void Players::player1hand () {
CardClass deckFunc;
deckFunc.deckFunction();
int p1Chips = 10;
int pot = 0;
int player1bet = 0;
srand(time(0));
char ans;
do {
int z = rand () % 52;
cout << deck[ z ].rank << " of " << deck[ z ].suit << endl;
cout << "Place bet: ";
cin >> player1bet;
if (player1bet > 0) {
pot = pot + player1bet;
p1Chips = p1Chips - player1bet;
cout << pot << endl;
cout << p1Chips << endl;
}
else {
cout << "Player 1 folds.";
cin >> ans;
}
cout << "Would you like another card? " << endl;
cin >> ans;
} while (ans == 'y');
}
int main()
{
Players player1;
player1.player1hand();
pause;
return 0;
}
Card.h文件(尚未真正使用):
#include <iostream>
using namespace std;
class Players
{
public:
void player1hand ();
void player2hand ();
void player3hand ();
void player4hand ();
void player5hand ();
void player6hand ();
private:
};
我知道一些功能和课堂互动现在有点草率但我只是想让一切运转起来。所以基本上,我需要玩家类从牌组中取一张牌,然后允许他们下注,然后重复这个过程。谢谢你的帮助!
答案 0 :(得分:0)
你有
struct card deck[52];
这定义了函数deck
中的CardClass::deckFunction
。这不会使deck
中显示Players::player1hand
。
您可以将deck
作为参数传递给player1Hand
来解决此问题。
更改Players
的成员函数以包含其他参数。
class Players
{
public:
void player1hand (card deck[]);
void player2hand (card deck[]);
void player3hand (card deck[]);
void player4hand (card deck[]);
void player5hand (card deck[]);
void player6hand (card deck[]);
private:
};
然后,更新函数的实现。更新的Players::player1hand
将如下所示:
void Players::player1hand (card deck[]) {
// ... include the body of the function
}
答案 1 :(得分:0)
你的
struct card deck[52];
仅在void CardClass::deckFunction ()
中可见,并且您尝试在void Players::player1hand()