C++警告 C4800 - “int”强制值为 bool“true”或“假”带 switch 语句

发布于 2024-10-26 09:01:30 字数 4406 浏览 3 评论 0原文

在这段代码中,当使用 player->giveOptions() 初始化时,toPurchase 从整数转换为布尔值(返回整数,而不是布尔值) )非常接近此代码的末尾。之后,有一个 switch(toPurchase),编译器会说它不起作用,因为 toPurchase 已转换为 bool。为什么?

int loc = player->giveOptions(4,"Trainers","Market","Inn","Back","");
    int chos;
    bool success = true;
    bool toPurchase = false;
    switch(loc){
    case 1:
        text.push_back("Hello, "+player->name+".");
        if (player->firstTrainerVisit){
            text.push_back("I'm not sure if anyone's told you, but there's been some strange talk about you lately...");
            text.push_back("Hmm... A lot of people are scared of you.");
            text.push_back("Anyway, nice to meet you. I'm Yobbee, the village trainer.");
            text.push_back("You'll need training up before meeting the village elders.");
            text.push_back("Usually, you need to pay to get trained by me, but as a first time bonus I'll teach you for free.");
            text.push_back("After you've been trained, you get a skill point, which you can use to increase one of your abilities.");
        }
        text.push_back("Would you like to be trained today?");
        NPCTalk("Yobbee (Trainer)",text);
        text.clear();
        chos = player->giveOptions(2,"Yes","No","","","");
        switch(chos){
        case 1:
            if(!player->firstTrainerVisit){
                cout << "This will cost 10 gold. Continue?" << endl;
                int purchase = player->giveOptions(2,"Yes","No","","","");
                if(purchase)
                    success = player->spendGold(5);
                else
                    success = false;
            }
            if (success){
                player->awardStat(1,"sp");
                cout << "After a day of vigorous training, you find your skills honed." << endl;
            }else{
                cout << "You don't have enough gold!" << endl;
            }
            if(player->firstTrainerVisit&&success){
                text.push_back("You can use your skill point to increase one of your stats by typing 'sp' any time you are given a choice.");
                text.push_back("There are other commands you can use at any choice opportunity as well - just type 'help' to see this one and others.");
                NPCTalk("Yobbee (Trainer)",text);
                text.clear();
                player->firstTrainerVisit = false;
            }
            break;
        case 2:
            break;
        }
        cout << "\nBack to the entrance to Poglathon..." << endl;
        system("PAUSE");
        Poglathon(text,player);
        break;
    case 2:
        if(player->firstMarketVisit){
            text.push_back("Oh... Hello, "+player->name+".");
            text.push_back("Ignore other people's looks. You'll find out about all this when you meet the Elders.");
            if(!player->firstElderVisit) text.push_back("Oh, you have already? Then I don't feel guilty about not explaining.");
            text.push_back("Here, at the market, you can buy new equipment for your travels, such as food, weapons and armour.");
            text.push_back("You don't have any gold at the moment, but as a standard first-time visit we will issue you with some leather armour and a rusty dagger.");
            text.push_back("Not the best of items, but it'll certainly help!");
            text.push_back("The weapon and armour are now in your backpack. To equip items in your backpack, when you are given an option type 'backpack'.");
            player->addToBackpackIfSpace("Rusty dagger");
            player->addToBackpackIfSpace("Leather armour");
            NPCTalk("Market Manager",text);
            text.clear();
            player->firstMarketVisit = false;
        }
        cout << "Do you want to purchase anything at the market?" << endl;
        toPurchase = player->giveOptions(2,"Yes","No","","","");
        switch(toPurchase){
        case 1:
            cout << "You can't purchase anything yet. NOOB" << endl;
            break;
        case 2:
            break;
        }
        cout << "\nBack to the entrance to Poglathon..." << endl;
        system("PAUSE");
        Poglathon(text,player);
        break;
    }

In this segment of code, toPurchase is converted from an integer to a bool when it is initialised with player->giveOptions() (which returns an integer, not a boolean) very near the end of this code. After, there is a switch(toPurchase), which then the compiler says won't work because toPurchase has been converted to a bool. Why?

int loc = player->giveOptions(4,"Trainers","Market","Inn","Back","");
    int chos;
    bool success = true;
    bool toPurchase = false;
    switch(loc){
    case 1:
        text.push_back("Hello, "+player->name+".");
        if (player->firstTrainerVisit){
            text.push_back("I'm not sure if anyone's told you, but there's been some strange talk about you lately...");
            text.push_back("Hmm... A lot of people are scared of you.");
            text.push_back("Anyway, nice to meet you. I'm Yobbee, the village trainer.");
            text.push_back("You'll need training up before meeting the village elders.");
            text.push_back("Usually, you need to pay to get trained by me, but as a first time bonus I'll teach you for free.");
            text.push_back("After you've been trained, you get a skill point, which you can use to increase one of your abilities.");
        }
        text.push_back("Would you like to be trained today?");
        NPCTalk("Yobbee (Trainer)",text);
        text.clear();
        chos = player->giveOptions(2,"Yes","No","","","");
        switch(chos){
        case 1:
            if(!player->firstTrainerVisit){
                cout << "This will cost 10 gold. Continue?" << endl;
                int purchase = player->giveOptions(2,"Yes","No","","","");
                if(purchase)
                    success = player->spendGold(5);
                else
                    success = false;
            }
            if (success){
                player->awardStat(1,"sp");
                cout << "After a day of vigorous training, you find your skills honed." << endl;
            }else{
                cout << "You don't have enough gold!" << endl;
            }
            if(player->firstTrainerVisit&&success){
                text.push_back("You can use your skill point to increase one of your stats by typing 'sp' any time you are given a choice.");
                text.push_back("There are other commands you can use at any choice opportunity as well - just type 'help' to see this one and others.");
                NPCTalk("Yobbee (Trainer)",text);
                text.clear();
                player->firstTrainerVisit = false;
            }
            break;
        case 2:
            break;
        }
        cout << "\nBack to the entrance to Poglathon..." << endl;
        system("PAUSE");
        Poglathon(text,player);
        break;
    case 2:
        if(player->firstMarketVisit){
            text.push_back("Oh... Hello, "+player->name+".");
            text.push_back("Ignore other people's looks. You'll find out about all this when you meet the Elders.");
            if(!player->firstElderVisit) text.push_back("Oh, you have already? Then I don't feel guilty about not explaining.");
            text.push_back("Here, at the market, you can buy new equipment for your travels, such as food, weapons and armour.");
            text.push_back("You don't have any gold at the moment, but as a standard first-time visit we will issue you with some leather armour and a rusty dagger.");
            text.push_back("Not the best of items, but it'll certainly help!");
            text.push_back("The weapon and armour are now in your backpack. To equip items in your backpack, when you are given an option type 'backpack'.");
            player->addToBackpackIfSpace("Rusty dagger");
            player->addToBackpackIfSpace("Leather armour");
            NPCTalk("Market Manager",text);
            text.clear();
            player->firstMarketVisit = false;
        }
        cout << "Do you want to purchase anything at the market?" << endl;
        toPurchase = player->giveOptions(2,"Yes","No","","","");
        switch(toPurchase){
        case 1:
            cout << "You can't purchase anything yet. NOOB" << endl;
            break;
        case 2:
            break;
        }
        cout << "\nBack to the entrance to Poglathon..." << endl;
        system("PAUSE");
        Poglathon(text,player);
        break;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必了 2024-11-02 09:01:30

您打开 toPurchase,它实际上是布尔值(在第 4 行中声明为布尔值)

you switch on toPurchase, which is in fact boolean (declared as boolean in line 4)

伴梦长久 2024-11-02 09:01:30

只需将此代码:更改

switch(toPurchase){
    case 1:
        cout << "You can't purchase anything yet. NOOB" << endl;
        break;
    case 2:
        break;
    }

为:

if (toPurchase)
{
    cout << "You can't purchase anything yet. NOOB" << endl;
}

Simply change this code:

switch(toPurchase){
    case 1:
        cout << "You can't purchase anything yet. NOOB" << endl;
        break;
    case 2:
        break;
    }

to:

if (toPurchase)
{
    cout << "You can't purchase anything yet. NOOB" << endl;
}
独夜无伴 2024-11-02 09:01:30

C++ 是一种静态类型语言。您已将 toPurchase 声明为 bool 变量,因此它将保持 bool 状态,直到您将 is 强制转换为其他变量。但不建议手动铸造。

虽然 int 可以隐式转换为 bool,这发生在
<代码>
toPurchase = 玩家->giveOptions(2,"是","否","","","");

这个赋值不会像在 php 中那样改变 toPurchase 的类型。它将 GiveOptions 的返回值转换为 bool,并将转换后的 bool 值分配给 Purchase。

C++ is a statically typed language. You have declared toPurchase as a bool variable, so it will remain bool until you violently cast is to something else. But manual casting is not recommended.

Although, int can be casted implicitly to bool, and this happens at

toPurchase = player->giveOptions(2,"Yes","No","","","");

.

This value assignent doesn't change the type of toPurchase like it would do in php for example. It casts the return value of giveOptions to bool, and assigns that casted bool value to to Purchase.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文