C++ Cout & Cin & System "Ambiguous"

29
我正在使用C++编程时,突然所有的“cout”和“cin”都出现了错误并显示为“不明确”。包括System。
我不知道为什么会发生这种情况。一切都很好,我已经编写同样的程序约2小时,当它突然发生时...
编辑:
我仍然可以运行程序而没有错误,但是它们在文本中显示为错误,即红色下划线。发生了什么?
我正在使用Visual Studio 2013 IDE,包括其自带的一切。
#include <iostream>
#include <ctime>
#include <string>
#include <Windows.h>
#include <cstdlib>
#include <stdlib.h>

using namespace std;

int main()
{

struct Gun
{
    string name;
    int damage;
    int cost;
    bool purchased;
    bool equipped;

} M4A1, FAMAS;

//WEAPONS INFO
M4A1.cost = 50;
M4A1.damage = 5;
M4A1.purchased = false;
M4A1.equipped = false;

FAMAS.cost = 300;
FAMAS.damage = 10;
FAMAS.purchased = false;
FAMAS.equipped = false;
//WEAPONS INFO

//-----PLAYER(BEGIN)-----

struct Player
{
    int health;
    string name;
    int money;
    int energy;

    string l_a;
    string r_a;
    string l_l;
    string r_l;

    string rank;

} Player;

//GAME PLAYER BEGIN

Player.l_a = "Normal";
Player.r_a = "Normal";
Player.l_l = "Normal";
Player.r_l = "Normal";

Player.health = 100;
Player.money = 100;
Player.energy = 100;

string plyrname;
string rank = "Private";

Player.name = plyrname;

//GAME PLAYER END

//-----PLAYER(END)-----

cout << "What is your name? ";
cin >> plyrname;

goto mmenu;


mmenu:

//-----MAIN MENU(BEGIN)-----
system("CLS");
if (Player.l_a == "Damaged")
{
    cout << "Your Left Arm is damaged! Sleep for a while to fix it!";
    Sleep(1600);
}
if (Player.r_a == "Damaged")
{
    cout << "Your Right Arm is damaged! Sleep for a while to fix it!";
    Sleep(1600);
}
if (Player.l_l == "Damaged")
{
    cout << "Your Left Leg is damaged! Sleep for a while to fix it!";
    Sleep(1600);
}
if (Player.r_l == "Damaged")
{
    cout << "Your Right Leg is damaged! Sleep for a while to fix it!";
    Sleep(1600);
}


if (Player.money >= 500 && Player.rank == "Private")
{
    system("CLS");
    cout << "You have been promoted to Private 2!";
    Player.rank = "Private 2";
    Sleep(1600);

}

if (Player.money >= 1000 && Player.rank == "Private 2")
{
    system("CLS");
    cout << "You have been promoted to Private First Class!";
    Player.rank = "Private First Class";
    Sleep(1600);
}


system("CLS");
cout << "Health: " << Player.health << ". Money: " << Player.money << " dollars." << "   Energy: " << Player.energy;
if (M4A1.equipped)
    cout << "\nGun: M4A1";
if (FAMAS.equipped)
    cout << "\nGun: FAMAS";

cout << "\n\nRank: " << Player.rank;
cout << "\n\n1) Go to Gunstore\n2) Sleep\n3) Fight\n\nAction: ";
int mmenuch1;
cin >> mmenuch1;

if (mmenuch1 == 1)
{
    goto gunstore;
}

if (mmenuch1 == 2)
{
    system("CLS");
    cout << "You sleep, restoring your energy.";
    Player.energy = 100;

    if (Player.l_a == "Damaged")
    {
        cout << "\n\nYour Left Arm was healed.";
        Player.l_a = "Normal";
    }
    if (Player.r_a == "Damaged")
    {
        cout << "\n\nYour Right Arm was healed.";
        Player.r_a = "Normal";
    }
    if (Player.l_l == "Damaged")
    {
        cout << "\n\nYour Left Leg was healed.";
        Player.l_l = "Normal";
    }
    if (Player.r_l == "Damaged")
    {
        cout << "Your Right Leg was healed.";
    }


    Sleep(1400);
    goto mmenu;
}

if (mmenuch1 == 3)
{
    system("CLS");
    goto fight;
}

//-----MAIN MENU(END)-----


fight:
srand(time(0));

system("CLS");

if (Player.r_a == "Damaged" || Player.r_l == "Damaged" || Player.l_a == "Damaged" || Player.l_l == "Damaged")
{
    cout << "You're injured, sleep to heal.";
    Sleep(1400);
    goto mmenu;
}

if (Player.energy < 40)
{
    cout << "You don't have enough energy to fight.";
    Sleep(1400);
    goto mmenu;
}

if (M4A1.equipped == false && FAMAS.equipped == false)
{
    cout << "You don't have a gun equipped.";
    Sleep(1400);
    goto gunstore;
}

if (M4A1.equipped == true && Player.energy > 40)
{

    int randnum1 = rand() % (M4A1.damage - 2 + 1) + 2;
    Player.money = Player.money + (randnum1 * 15);
    Player.energy = Player.energy - 40;

    int randnum3 = rand() % (10 - 1 + 1) + 1;
    if (randnum3 < 4)
    {

        int randnum4 = rand() % (13 - 1 + 1) + 1;

        if (randnum4 < 3)
        {
            Player.l_a = "Damaged";
        }
        if (randnum4 <= 6 && randnum4 >= 4)
        {
            Player.r_a = "Damaged";
        }
        if (randnum4 <= 9 && randnum4 >= 7)
        {
            Player.l_l = "Damaged";
        }
        if (randnum4 <= 13 && randnum4 >= 10)
        {
            Player.r_l = "Damaged";
        }



    }


    cout << "You fight, killing " << randnum1 << " enemies, making " << randnum1 * 15 << " dollars!";
    Sleep(1600);
    goto mmenu;





}




if (FAMAS.equipped == true && Player.energy > 40)
{
    int randnum2 = rand() % (FAMAS.damage - 4 + 1) + 4;
    Player.money = Player.money + (randnum2 * 15);
    Player.energy = Player.energy - 40;




    int randnum5 = rand() % (10 - 1 + 1) + 1;
    if (randnum5 < 4)
    {

        int randnum6 = rand() % (13 - 1 + 1) + 1;

        if (randnum6 < 3)
        {
            Player.l_a = "Damaged";
        }
        if (randnum6 <= 6 && randnum6 >= 4)
        {
            Player.r_a = "Damaged";
        }
        if (randnum6 <= 9 && randnum6 >= 7)
        {
            Player.l_l = "Damaged";
        }
        if (randnum6 <= 13 && randnum6 >= 10)
        {
            Player.r_l = "Damaged";
        }



    }



    cout << "You fight, killing " << randnum2 << " enemies, making " << randnum2 * 15 << " dollars!";
    Sleep(1600);
    goto mmenu;
}



//-----GUNSTORE(BEGIN)-----
gunstore:
system("CLS");
cout << "Welcome to the gunstore! You have " << Player.money << " dollars.";
cout << "\n\n1)M4A1 | Assault Rifle | $50\n2)FAMAS | Assault Rifle | $300\n\n3)Back\n\nAction: ";

int gschoice1;

cin >> gschoice1;

if (gschoice1 == 1)
{
    goto prchs_M4A1;
}
else if (gschoice1 == 2)
{
    goto prchs_FAMAS;
}
else if (gschoice1 == 3)
{
    goto mmenu;
}


prchs_M4A1:

system("CLS");

if (M4A1.purchased == true)
{
    cout << "You already purchased the M4A1. Would you like to equip it?\n\n1)Yes\n2)No\n\nAction: ";
    int gschoice6;
    cin >> gschoice6;

    if (gschoice6 == 1)
    {
        system("CLS");
        M4A1.equipped = true;
        FAMAS.equipped = false;
        goto mmenu;
    }
    else if (gschoice6 == 2)
    {
        goto gunstore;
    }


}


if (Player.money >= 0)
{
    system("CLS");
    cout << "Would you like to buy the M4A1?";
    cout << "\n\n1)Yes\n2)No\n\nAction: ";

    int gschoice2;
    cin >> gschoice2;

    if (gschoice2 == 1)
    {
        system("CLS");
        Player.money = Player.money - M4A1.cost;
        M4A1.purchased = true;
        cout << "You've purchased the M4A1. Would you like to equip it?\n\n1)Yes\n2)No\n\nAction: ";
        int gschoice3;
        cin >> gschoice3;



        if (gschoice3 == 1)
        {
            system("CLS");
            M4A1.equipped = true;
            FAMAS.equipped = false;

            cout << "You've equipped the M4A1";
            Sleep(1400);
            goto gunstore;
        }

        if (gschoice3 == 2)
        {
            system("CLS");
            M4A1.equipped = false;
            goto gunstore;
        }


    }

    if (gschoice2 == 2)
    {
        system("CLS");
        goto gunstore;
    }

}
else if (Player.money < 0)
{
    system("CLS");
    cout << "You don't have enough money.";
    Sleep(1400);
    goto gunstore;
}

prchs_FAMAS:

if (FAMAS.purchased == true)
{
    cout << "You already purchased the FAMAS. Would you like to equip it?\n\n1)Yes\n2)No\n\nAction: ";
    int gschoice7;
    cin >> gschoice7;

    if (gschoice7 == 1)
    {
        system("CLS");
        FAMAS.equipped = true;
        M4A1.equipped = false;
        goto mmenu;
    }
    else if (gschoice7 == 2)
    {
        goto gunstore;
    }
}



    if (Player.money >= 100)
    {
        system("CLS");
        cout << "Would you like to buy the FAMAS?";
        cout << "\n\n1)Yes\n2)No\n\nAction: ";

        int gschoice4;
        cin >> gschoice4;

        if (gschoice4 == 1)
        {
            system("CLS");
            Player.money = Player.money - FAMAS.cost;
            FAMAS.purchased = true;
            cout << "You've purchased the FAMAS. Would you like to equip it?\n\n1)Yes\n2)No\n\nAction: ";
            int gschoice5;
            cin >> gschoice5;

            if (gschoice5 == 1)
            {
                system("CLS");
                FAMAS.equipped = true;
                M4A1.equipped = false;
                cout << "You've equipped the FAMAS";
                Sleep(1400);
                goto gunstore;
            }

            if (gschoice5 == 2)
            {
                system("CLS");
                FAMAS.equipped = false;
                goto gunstore;
            }


        }

        if (gschoice4 == 2)
        {
            system("CLS");
            goto gunstore;
        }

    }
    else if (Player.money < 100)
    {
        system("CLS");
        cout << "You don't have enough money.";
        Sleep(1400);
        goto gunstore;
    }
    //-----GUNSTORE-----




}

2
不,这不是“偶然发生”的。你改变了什么! - Lightness Races in Orbit
1
我知道 coutcin 很可能是什么。那么 System 是什么? - T.C.
4
“红色涂抹线”指的是某种集成开发环境中的智能感知功能。请注意,有很多这样的开发环境,许多程序员甚至不使用集成开发环境。因此,如果您的问题涉及到某个集成开发环境的行为,请在问题中明确注明“哪一个”。 - Lightness Races in Orbit
8
现在是一个大量代码的转储。 :( 我不想打击你,@Colby,因为我知道你是一位新手年轻程序员。我希望你继续尝试和实验,因为这是_好的_!不过,你应该意识到,你的问题可能不会在SO上找到太多支持,因为它不是一个从零开始教学的网站,而且你的问题显示出了不遵循最佳实践的模式。我建议你阅读一下帮助材料和一些得分较高的问题,看看我们在SO上做什么。祝你好运! - Lightness Races in Orbit
4
为什么你同时包含了 <cstdlib><stdlib.h> - T.C.
显示剩余11条评论
1个回答

32
这种事情不是自己魔法般地发生的; 你改变了什么! 在工业界,我们使用“版本控制”进行定期保存点,因此当出现问题时,我们可以追溯到导致该问题的具体更改。
由于您在这里没有这样做,我们只能猜测。在Visual Studio中,Intellisense(提供自动完成下拉菜单和那些波浪红线的技术)与实际的C++编译器分开工作,在引擎盖下运行,有时会有些错误。
在这种情况下,我会问你为什么同时包含cstdlibstdlib.h; 你只应该使用其中之一,我建议使用前者。它们基本上是相同的头文件,是C头文件,但cstdlib将它们放在std命名空间中,以便“C++ - ise”它们。理论上,包含两个不会冲突,但是,嗯,我们所说的是微软。他们的C++工具链有时留下了一些不足之处。每当Intellisense与编译器不一致时,都必须被视为一个bug,无论从哪个角度看!
无论如何,你的using namespace std的用法(我建议你以后不要这样做)意味着 std::system 来自 cstdlib 现在与来自 stdlib.h 的 system 冲突。我无法解释std :: coutstd :: cin的情况。 尝试删除#include <stdlib.h>并查看发生了什么。 如果您的程序成功构建,则不需要太担心,但是我可以想象当您在IDE中工作时,虚警会很烦人。

3
我通常不会回答这样低质量的问题,但正如我在上面的评论中所指出的,我想_鼓励_新的年轻程序员继续实验和学习。然而,由于Stack Overflow不是一个论坛或讨论区域,我建议你查看帮助区域和任何现有的_upvoted_ C++问题,以了解我们在Stack Overflow上做什么! - Lightness Races in Orbit
1
我的理论是Intellisense可能会报告一个歧义,例如::systemstd::system之间的歧义,这要归功于using namespace std; - T.C.
7
在 VS 2017 中,我遇到了同样的问题。通过将代码与源代码控制进行对比,确认没有进行任何更改。然后重新启动了 VS 并打开了我的项目,问题就消失了。 - Chris Arbogast
3
同样的问题在VS2019中发生过。注释掉#include <iostream>,编译(出现了大量错误),取消注释,重新编译就解决了所有问题。重启VS也可能会解决这个问题。我猜是Intellisense的某种bug。 - AlainD
2
我在VS 2020中遇到了同样的情况。我重新启动了IDE,问题就解决了。 - Naveen
显示剩余14条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接