C++:变量"f"未初始化

c++: variable 'f' not initialized

本文关键字:初始化 C++ 变量      更新时间:2023-10-16

我正在用c++创造一款基于文本的Risk游戏。一直试图实现骰子/战斗功能,但它一直给我的错误"f是使用没有被初始化!"这很奇怪,因为我之前初始化了它。这是我的代码

void battle(int attack, int defend)
{
    int a, b, c, d, e, f;
    if (attack >= 3)
    {
        a = rollDice();
        b = rollDice();
        c = rollDice();
    }
    else if (attack == 2)
    {
        a = rollDice();
        b = rollDice();
        c = 0;
    }
    else if (attack == 1)
    {
        a = rollDice();
        b = 0;
        c = 0;
    }
    else if (defend >= 2)
    {
        d = rollDice();
        e = rollDice();
        f = 0;
    }
    else if (defend == 1)
    {
        d = rollDice();
        e = 0;
        f = 0;
    }
    sortValues(a, b, c);
    sortValues(d, e, f);
    cout << endl << "The attacking country rolled the following dices: " << a
            << " " << b << " " << c << ".";
    cout << endl << "The defending country rolled the following dices: " << d
            << " " << e << " " << f << ".";
    if (a == d)
    {
        attack = attack - 1;
    }
    else if (a != d)
    {
        if (a < d)
        {
            attack = attack - 1;
        }
        else if (a > d)
        {
            defend = defend - 1;
        }
    }
    else if (b == e)
    {
        attack = attack - 1;
    }
    else if (b != e)
    {
        if (b < e)
        {
            attack = attack - 1;
        }
        else if (b > e)
        {
            defend = defend - 1;
        }
    }
// since the int f is never used, this function is critical to implement. so that we can avoid compile time error
    _unused(f);
}
int main()
{
    int attack;
    int defend;
    int choice;
// this asks for the user choice of what he would like to do. 
    cout << "How large is the attacking army?" << endl;
    cin >> attack;
    cout << "How large is the defending army?" << endl;
    cin >> defend;
    while (attack >= 1 && defend >= 1)
    {
        cout
                << "Based on the numbers you entered, you have a couple of choices: "
                << endl << endl;
        cout << "1) Battle once" << endl;
        cout << "2) Battle until the end" << endl;
        cout << "3) Withdraw from the battle" << endl;
        cout << "Please enter your game choice (just enter the number): "
                << endl;
        cin >> choice;
        if (choice == 1)
        {
            battle(attack, defend);
        }
        if (choice == 2)
        {
            do
            {
                battle(attack, defend);
            } while (attack != 0 && defend != 0);
            cout << "Battle ended. The attacking forces are: " << attack
                    << " , while the defending forces are: " << defend << endl;
        }
        if (choice == 3)
        {
            break;
        }
    }
    if (attack < 1)
    {
        cout
                << "The attacking forces have lost. Defending forces keep their territory"
                << endl;
    }
    else if (defend < 1)
    {
        cout << "The attacking forces have won over the territory." << endl;
    }
    system("pause");
    return 0;
}

这不是所有的代码,但它是临界区,包括并使用了f。

battle函数中,您正在调用sortValues(d,e,f),但在前3个if-case中,f没有获得值(因此未初始化)。

实际上你应该得到一个类似的错误(它真的是一个错误或警告?)为d和在某些情况下,你不给a, bc的值。