2011-03-27 31 views
0

在這段代碼中,當被初始化爲player->giveOptions()(它返回一個整數,而不是布爾值)非常接近此代碼的結尾。之後,有一個switch(toPurchase),然後編譯器說不起作用,因爲toPurchase已被轉換爲布爾值。爲什麼?C++警告C4800 - 'int'通過switch語句將值強制爲bool'true'或'false'

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; 
    } 

回答

2

你上toPurchase切換,這實際上是布爾型(第4行聲明爲布爾值)

+2

什麼,我聲明'購買'作爲一個布爾?!這很煩人,我花了很多年試圖弄清楚。我怎麼沒有注意到這一點? – pighead10 2011-03-27 09:54:13

2

只需改變這種代碼:

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; 
} 
0

C++是一種靜態類型語言。您已將購買聲明爲布爾變量,因此它將保持布爾狀態,直到您猛烈投射其他內容爲止。但不建議手動鑄造。

雖然,int可以隱式地轉化爲bool,並且這發生在 toPurchase = player->giveOptions(2,"Yes","No","","","");

這個值assignent不會改變toPurchase的類型,就像它會在php中做的那樣。它將giveOptions的返回值轉換爲bool,並將該鑄造的bool值分配給Purchase。