为什么我不断重新定义形式参数错误

时间:2015-04-07 21:12:36

标签: c++ user-defined-functions redefinition

#include <iostream>
using namespace std;
void  mf_option(int player1, int player2);
void  mf_option2(int player1, int player2);
void  mf_option3(int player1, int player2);


int main()
{
    int player1, player2;
    cout << "\n\n\nWlcome to \"Rock Paper Scissor game\"\n";

    cout << " Type 0 for rock, 1 for paper , and 2 for scissors\n";
    cout << "Player 1, choose 0, 1 or 2: ";
    cin >> player1;
    system("clear");
    cout << " Type 0 for rock, 1 for paper , and 2 for scissors\n";
    cout << "Player 2, choose 0, 1 or 2: ";
    cin >> player2;
    system("clear");

    cout << "\n\nplayer 1, you chose " << player1 << " and player 2 you        chose " << player2 << "\n\n";
    mf_option(player1, player2);
    mf_option2(player1, player2);
    mf_option3(player1, player2);
    return 0;
}

void mf_option(int player1, int player2)
{
    int player1, player2;
    if (player1 == 0)
    {
        if (player2 == 0)
            cout << "It's a tie!\n\n\n\n";
        else if (player2 == 1)
            cout << "Paper Beat rock! Player2 wins!\n\n\n\n";
        else if (player2 == 2)
            cout << "Rock beat scissors! Player 1 wins!\n\n\n\n";
    }
}
void mf_option2(int player1, int player2)
{
    int player1, player2;
    if (player1 == 1)
    {
        if (player2 == 0)
            cout << "Paper beat rock! Player 1 wins!\n\n\n\n";
        else if (player2 == 1)
            cout << "It's a tie!\n\n\n\n";
        else if (player2 == 2)
            cout << "Scissors beat paper! Player 2 wins!\n\n\n\n";
    }
}
 void mf_option3(int player1, int player2)
{
    int player1, player2;
if (player1 == 2)
    {
        if (player2 == 0)
            cout << "Rock beat scissors! Player 2 wins!\n\n\n\n";
        else if (player2 == 1)
            cout << "Scissors beat paper! Player 1 wins!\n\n\n\n";
        else if (player2 == 2)
            cout << "its a tie!\n\n\n\n";
    }
}

当我尝试运行这个程序时,它说我重新定义了正式的参数错误。第35,48,61行发生错误。该错误适用于player1和player2。有人也可以解释用户定义函数的要点

1 个答案:

答案 0 :(得分:4)

问题在于:

void mf_option(int player1, int player2)
{
    int player1, player2;
...

您正在定义与传入的变量名称相同的变量。