【问题标题】:C++ beginner here is it possible to call a function within another function?这里的 C++ 初学者可以在另一个函数中调用一个函数吗?
【发布时间】:2017-09-24 19:37:52
【问题描述】:
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

void displayRules();
void play();
int shuffleCard(int cardPile[]);


int main()
{
    int board[26] = {0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0};
    int cardPile[10] = {1, 1, 2, 2, 3, 3, 4, 4, 0, 5};
    int player1 = 0;
    int player2 = 0;

    play();


    return 0;
}

void play(){
    displayRules();
    shuffleCard(cardPile);

}

void displayRules(){
    cout << "\nWelcome to GoHome! The main objective of this game is to reach Home"
            " first." << endl;
    cout << "The basic rules of the game are as follows:" << endl;
    cout << "\n-To begin the player with the shortest name goes first." << endl;
    cout << "-Each player picks a card that has a number on it and the player"
            " must moves forward that many number of spaces." << endl;
    cout << "-If a card says 'Lose A Turn', the player does nothing and the"
            "turn moves to the next player." << endl;
    cout << "-If a card says 'Switch Places', that player is allowed to switch"
            " places with any player on the board." << endl;
    cout << "-If a player lands on an obstacle, that player must move back that"
            " many number of spaces." << endl;
    cout << "-If a player lands another obstacle while moving backwards, then it"
            " does not have to move backwards again.\n"<<endl;
}

int shuffleCard(int cardPile[]){

    srand(time(0));
    for(int i = 0; i < 10; i++){
            int size = rand() % 10;
        cout << cardPile[size] << endl;
    }
}

我正在做一个家庭作业,我的教授特别提到他只希望 main 调用 play 函数,而其他一切都应该在函数中完成。

基本上他希望函数调用其他函数。到目前为止,我有 2 个函数,一个称为 play,另一个称为 shuffleCard。我的问题是我不确定如何让 play 函数调用 shuffleCard 函数。 play 函数调用 displayRules 函数没有问题,但是当我尝试编译它时,我收到一个错误,提示 use of undeclared identifier 'cardPile'.

【问题讨论】:

  • 当然可以。您可能错过了指定前向声明?
  • 打开你的 C++ 书籍,阅读解释“前向声明”如何工作的章节。
  • 当编译器显示use of undeclared identifier 'cardPile' 时,这意味着在play 内部您使用的是名称(标识符)cardPile,但它找不到任何这样的变量。您在main 内部有一个,但无法从外部main 访问。您需要创建另一个局部变量int cardPile[10] = {1, 1, 2, 2, 3, 3, 4, 4, 0, 5}; 或将cardPile 传递给play,以便它可以将其传递给shuffleCard
  • 好的,谢谢你们,我知道我做错了什么,现在开始工作了!

标签: c++ macos


【解决方案1】:
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

void displayRules();
void play(int cardPile[]);
int shuffleCard(int cardPile[]);


int main()
{
    int board[26] = {0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0};
    int cardPile[10] = {1, 1, 2, 2, 3, 3, 4, 4, 0, 5};
    int player1 = 0;
    int player2 = 0;

    play(cardPile);


    return 0;
}

void play(int cardPile[]){
    displayRules();
    shuffleCard(cardPile);

}

void displayRules(){
    cout << "\nWelcome to GoHome! The main objective of this game is to reach Home"
            " first." << endl;
    cout << "The basic rules of the game are as follows:" << endl;
    cout << "\n-To begin the player with the shortest name goes first." << endl;
    cout << "-Each player picks a card that has a number on it and the player"
            " must moves forward that many number of spaces." << endl;
    cout << "-If a card says 'Lose A Turn', the player does nothing and the"
            "turn moves to the next player." << endl;
    cout << "-If a card says 'Switch Places', that player is allowed to switch"
            " places with any player on the board." << endl;
    cout << "-If a player lands on an obstacle, that player must move back that"
            " many number of spaces." << endl;
    cout << "-If a player lands another obstacle while moving backwards, then it"
            " does not have to move backwards again.\n"<<endl;
}

int shuffleCard(int cardPile[]){

    srand(time(0));
    for(int i = 0; i < 10; i++){
            int size = rand() % 10;
        cout << cardPile[size] << endl;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 2021-07-14
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    相关资源
    最近更新 更多