功能原型干扰了编译器的定义?

时间:2020-04-07 06:46:23

标签: c++ function prototype void

我正在尝试为“ Rock,Paper,剪刀,Lizard,Spock”​​游戏编写代码,作为课堂实验的一部分。在其他程序中,我使用了函数原型,但取得了一些成功,但是当我这样做时,它是行不通的。如果我将void函数放在main之上,则编译器会读取它们,但是我需要在之后对其进行定义。我试图像这样初始化它们

#include <iostream> // basic i/o
#include <iomanip> // output formatting
#include <cmath> // math functions
#include <cstdlib> // rand() and srand()
#include <ctime> // system time
#include <string> // string handling

using namespace std;


void playerRock();
void playerPaper();
void playerScissors();
void playerLizard();
void playerSpock();



int main(){...

这在其他程序中没有引起任何问题,我已经看到我的教授和教科书都做了这件事,但是我的编译器似乎无法将以下内容识别为函数定义,因此我没有确定为什么

int main(){...
}

void playerRock(void){
    cout << "Player chooses Rock" << endl;
    if(opponentChoice == 1){
        cout << "Opponent chooses Paper" << endl;
        cout << "Paper covers Rock!" << endl;
        opponentScore++;
    }
    if(opponentChoice == 2){
        cout << "Opponent chooses Scissors" << endl;
        cout << "Rock smashes Scissors!" << endl;
        playerScore++;
    }
    if(opponentChoice == 3){
        cout << "Opponent chooses Lizard" << endl;
        cout << "Rock smashes Lizard" << endl;
        playerScore++;
    }
    else{
        cout << "Opponent chooses Spock" << endl;
        cout << "Spock vaporizes Rock!" << endl;
        opponentScore++;
    }
    cout << "Opponent Score: " << opponentScore << endl;
    cout << "Player Score: " << playerScore << endl;

    return;
}

void playerPaper(void){
    cout << "Player chooses Paper!" << endl;
    if(opponentChoice == 0){
        cout << "Opponent chooses Rock" << endl;
        cout << "Paper covers Rock!" << endl;
        playerScore++;
    }
    if(opponentChoice == 2){
        cout << "Opponent chooses Scissors" << endl;
        cout << "Scissors cuts Paper!" << endl;
        opponentScore++;
    }
    if(opponentScore == 3){
        cout << "Opponent chooses Lizard" << endl;
        cout << "Lizard eats Paper!" << endl;
        opponentScore++;
    }
    else{
        cout << "Opponent chooses Spock" << endl;
        cout << "Paper disproves Spock!" << endl;
        playerScore++;
    }
    cout << "Opponent Score: " << opponentScore << endl;
    cout << "Player Score: " << playerScore << endl;

    return;
}

void playerScissors(void){
    cout << "Player chooses Scissors" << endl;
    if(opponentChoice == 0){
        cout << "Opponent chooses Rock" << endl;
        cout << "Rock smashes Scissors" << endl;
        opponentScore++;
    }
    if(opponentChoice == 1){
        cout << "Opponent chooses Paper" << endl;
        cout << "Scissors cuts Paper!" << endl;
        playerScore++;
    }
    if(opponentChoice == 3){
        cout << "Opponent chooses Lizard" << endl;
        cout << "Scissors decapitate Lizard!" << endl;
        playerScore++;
    }
    else{
        cout << "Opponent chooses Spock" << endl;
        cout << "Spock smashes Scissors!" << endl;
        opponentScore++;
    }
    cout << "Opponent Score: " << opponentScore << endl;
    cout << "Player Score: " << playerScore << endl;

    return;

}

void playerLizard(void){
    cout << "Player chooses Lizard" << endl;
    if(opponentChoice == 0){
        cout << "Opponent chooses Rock" << endl;
        cout << "Rock smashes Lizard" << endl;
        opponentScore++;
    }
    if(opponentChoice == 1){
        cout << "Opponent chooses Paper" << endl;
        cout << "Lizard eats Paper!" << endl;
        playerScore++;
    }
    if(opponentChoice == 2){
        cout << "Opponent chooses Scissors" << endl;
        cout << "Scissors decapitates Lizard" << endl;
        opponentScore++;
    }
    else{
        cout << "Opponent chooses Spock" << endl;
        cout << "Lizard poisons Spock!" << endl;
        playerScore++;
    }
    cout << "Opponent Score: " << opponentScore << endl;
    cout << "Player Score: " << playerScore << endl;

    return;
}

void playerSpock(void){
    cout << "Player chooses Spock" << endl;
    if(opponentChoice == 0){
        cout << "Opponent chooses Rock" << endl;
        cout << "Spock vaporizes Rock!" << endl;
        playerScore++;
    }
    if(opponentChoice == 1){
        cout << "Opponent chooses Paper" << endl;
        cout << "Paper disproves Spock" << endl;
        opponentScore++;
    }
    if(opponentChoice == 2){
        cout << "Opponent chooses Scissors" << endl;
        cout << "Spock breaks Scissors!" << endl;
        playerScore++;
    }
    else{
        cout << "Opponent chooses Lizard" << endl;
        cout << "Lizard poisons Spock!" << endl;
        opponentScore++;
    }
    cout << "Opponent Score: " << opponentScore << endl;
    cout << "Player Score: " << playerScore << endl;

    return;
}

我想我对为什么它可以在其他程序中起作用但在这里不起作用感到困惑?

1 个答案:

答案 0 :(得分:0)

函数的声明在使用时必须可见。如果您在playerRock中调用main,则在playerRock之前必须至少有main声明。您可以像其他操作一样,在其他位置(例如,main之后定义(定义函数的实际主体) 。

void playerRock();

这是playerRock声明

void playerRock() {
 // something
}

这是playerRock定义

还请注意,在c ++中,您不需要在函数参数列表中使用void