2015-07-03 94 views
-1

我一直在与我认为可能是一个简单的C++问题奋斗了几个小时,我真的可以使用一些帮助。这里是我想要做什么:C++ - 或字符串比较不断抛出布尔错误

void setRarity(std::string inRarity) 
{ 

    if (inRarity == "c" || inRarity == "C" || inRarity == "Common" || inRarity = "common") 
    { 
     rarity = "common"; 
    } 
    //additional else-if statements following the same type of syntax as above 

    return; 
} 

但我不断收到错误,当我尝试编译(在Ubuntu使用G ++):

In file included from main.cpp:6:0: 
class_Card.cpp: In member function ‘void Card::setRarity(std::string)’: 
class_Card.cpp:210:68: error: no match for ‘operator||’ (operand types are ‘bool’ and ‘std::string {aka std::basic_string<char>}’) 
    if (inRarity == "c" || inRarity == "C" || inRarity == "Common" || inRarity = "common") 
                    ^
class_Card.cpp:210:68: note: candidate is: 
class_Card.cpp:210:68: note: operator||(bool, bool) <built-in> 
class_Card.cpp:210:68: note: no known conversion for argument 2 from ‘std::string {aka std::basic_string<char>}’ to ‘bool’ 

+3

'='应该是'==' – Biffen

+0

@Biffen:就是这样!盯着屏幕的几个小时,我没有注意到我错过了一个等号!谢谢! – XSSerra

+0

嗯,第二个片段有==和parens,为什么不是一个答案Biffen,让人们不要试图回答 –

回答

3

在原来的片段中,的问题,我看到的是,你应该改变

if (inRarity == "c" || inRarity == "C" || inRarity == "Common" || inRarity = "common") 

if (inRarity == "c" || inRarity == "C" || inRarity == "Common" || inRarity == "common") 

(啊,有什么差异的单=可以使...)

+0

就是这样史蒂夫!那个单独的角色让我沉没 - 很好看! – XSSerra

+0

@XSSerra为什么第二个例子有错误? –

+0

@GlennTeitelbaum,第二个例子不会导致错误 – asimes

2

错误消息是很清楚的,你在最后一次检查拼写错误==

if (inRarity == "c" || inRarity == "C" || inRarity == "Common" || inRarity = "common") 
    { 
     rarity = "common"; 
    } 

而且由于inRarity="common"具有string类型,并且您正在尝试将它或它转换为布尔表达式,它告诉您没有匹配的||运算符。