0

我的 C++ 代码出现错误。我通常是一名 Java 开发人员,但我正在学习 C++ 课程,以便在我的编程能力方面更加广泛。无论如何,当我尝试构建此解决方案时,我得到了错误

1>c:\users\westlakes\documents\visual studio 2010\projects\blackjack\blackjack\playblackjack.cpp(81): error C3861: 'initializeGame': identifier not found
1>c:\users\westlakes\documents\visual工作室 2010\projects\blackjack\blackjack\playblackjack.cpp(82): error C3861: 'deal': identifier not found
1>c:\users\westlakes\documents\visual studio 2010\projects\blackjack\blackjack\playblackjack.cpp (83): 错误 C3861: 'playGame': 未找到标识符

谁能帮我吗?

// Programmer:  Matt Westlake

// Course:  COMP220

// Assignment:  Two-Dimensional Arrays

// Description:  The program will use a 2D array and a random-number
// generation to play Blackjack and keep track of a playing-card deck.

// Input:  User data entry and a playing-card deck represented as a two-
// dimensional array

// Output:  A screen display showing the current card hands of each player
// and the dealer, their score, win and lose status, and a final representation
// of the card deck after the game is over

#include <iostream>
#include <iomanip>
#include <windows.h>

using namespace std;

char cPlay = 'Y';       //Character variable for play game input
char tempDealerHand;
char cCardDeck[4][13];  //Character array representing the card deck
char output[5][8];
char card[1][1];
int iCard;              //Card array index
char cardName;
//0 = 2 card                                                              
//1 = 3 card
//2 = 4 card
//3 = 5 card
//4 = 6 card
//5 = 7 card                                                           
//6 = 8 card                                                        
//7 = 9 card                                     
//8 = 10 card                                                       
//9 = jack card
//10 = queen card
//11 = king card
//12 = ace card
int iNumberOfDraws = 0; //Number of rounds of card draws
int iSuit;//Suit array index
char Suit [4]; 

 //0 = diamonds
//1 = hearts
//2 = clubs
//3 = spades

// ASCII character display reference for display card suit symbols
//3 = heart symbol
//4 = diamond symbol
//5 = club symbol
//6 = spade symbol

int iNumberOfPlayers = 0;//Number of players in current game
int iPlayerCount[5];      //Integer array to holder each player's count
                                   //iPlayer[0] is always the dealer
int iHighestCount = 0;  //Highest count for a single game
int k, m;//integer loop counters


void initializeGame();
void deal();
void playGame();
char generateCard();
void generateDisplay();


void initializeGame()
{
if (iNumberOfPlayers == 0)
{
    cout << "No players in game, exiting program";
    Sleep(3000);
    exit(0);
}

cout << iNumberOfPlayers + "Joining the game with the dealer" << endl;
output[0][0] = 'Deal';
char name;
for (int i = 1; i <= iNumberOfPlayers; i++)
{
    output[i][0] = 'P ' + i;
}
for (int i = 0; i < 4; i++)
{
    for (int j = 0; j < 13; j++)
    {
        cCardDeck[i][j] = ' ';
    }
}

srand(GetTickCount());  //Seed the random-number generator
}

void deal()
{
for (k = 0; k <= iNumberOfPlayers; k++)
{
    for (m = 1; m <= 2; m++)
    {
        output[k][m] =  generateCard();
    }
}
tempDealerHand = output[0][0];
// output[0][0] = ? // to set dealer first hand to unknown
generateDisplay();
}

void playGame()
{
char HorS;
for (k = 1; k <= iNumberOfPlayers; k++)
{
    for (m = 3; m <= 5; m++)
    {
        cout << "Player " << k << " Would you like to Hit or Stay? Type H for Hit or anything else for Stay: ";
        cin >> HorS;
        if (HorS == 'H' || HorS == 'h')
        {
            output[k][m] = generateCard();
            generateDisplay();
        }
        else
        {
            break;
        }
    }
}
int dealerDraw = rand() % 3; // dealer will draw 0 to 3 cards;
for (int i = 0; i < dealerDraw; i++)
{
    output[0][i+3] = generateCard();
    generateDisplay();
}
}

char generateCard()
{
iSuit = rand() % 4;
iCard = rand() % 13;
if (iCard <= 8)
{
    cardName = iCard + 2;
}
else if (iCard == 9)
{
    cardName = 'J';
}
else if (iCard == 10)
{
    cardName = 'Q';
}
else if (iCard == 11)
{
    cardName = 'K';
}
else if (iCard == 12)
{
    cardName = 'A';
}
card[0][0] = Suit[iSuit],cardName;
return card[0][0];
}

void generateDisplay()
{ 
for (int i = 0; i < iNumberOfPlayers; i++)
{

    for (int j = 0; j < 8; j++)
    {
    cout << output[i][j];
    }
    cout << endl;
}
}

void main (void)
{


cout << "Do you want to play blackjack?";
cin >> cPlay;
Suit [0] = '0x03';
Suit [1] = '0x04';
Suit [2] = '0x05';
Suit [3] = '0x06';

cout << "Enter number of players: ";
cin >> iNumberOfPlayers;

while (true)
{
    initializeGame();
    deal();
    playGame();
}
}

编辑已更改,因此 main 位于底部,但仍然出现错误。

4

4 回答 4

2

您可以在 main 函数之前进行前向声明:

void initializeGame();
void deal();
void playGame();

void main (void)
{
  ...
}

void initializeGame()
{
  ...
}

void deal()
{
  ...
}

void playGame()
{
  ...
}

或将主函数定义放在文件底部。

于 2012-07-12T10:26:25.830 回答
1

您更新的代码似乎对我很好。这是屏幕截图。截屏

我在您的代码中找不到问题所在。看来您正在使用 Visual Studio。尝试清理您的解决方案(构建-> 清理解决方案),然后尝试重建您的代码。

于 2012-07-12T13:23:34.293 回答
0

通常,您需要按照调用它们的顺序放置标识符(即函数定义):如果您调用某物,则需要首先定义该物。在这种情况下,基本上把你的程序颠倒过来会对你有所帮助。

于 2012-07-12T10:19:16.110 回答
0

在 main() 之前复制所有函数,或者通常总是在 .h 文件中声明函数并在 .cpp 文件中定义它。

于 2012-07-12T10:20:25.277 回答