【问题标题】:Why do I keep getting a redefinition of formal parameter error为什么我不断收到重新定义形参错误
【发布时间】:2015-04-07 21:12:36
【问题描述】:
#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。有人也可以解释一下用户定义函数的意义

【问题讨论】:

  • 你应该学会阅读编译器警告/错误信息。

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


【解决方案1】:

问题出在这里:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    相关资源
    最近更新 更多