干扰了原型的函数原型,看到了定义吗?

问题描述 投票:0回答:1

我正在尝试为“ 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;
}

我想我为什么在其他程序中不能用,但是在这里不明白?

c++ function prototype void
1个回答
0
投票

函数的声明在使用时必须可见。如果在playerRock中调用main,则在playerRock之前必须至少具有maindeclaration。您可以define(编写函数的实际主体)在其他位置,例如main之后,就像您所做的一样。

void playerRock();

这是playerRock声明

void playerRock() {
 // something
}

这是playerRock定义

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

© www.soinside.com 2019 - 2024. All rights reserved.