我正在使用Code :: Blocks IDE,每当我尝试构建文件时,此代码都会引发错误:
{
cin >> input ;
locale loc;
for (string::size_type i=0; i<input.length(); ++i)
input[i] = tolower(input[i],loc);}
{
if (input == "not")
"" != "";
else if (input == "and")
"" && "";
else if (input == "or")
"" || "";
else if (input == "yes")
input = true;
else if (input == "no")
input = false;
}
当我尝试使单词“ no”等于布尔运算符false时发生错误。 这会引发此错误:
Projects\Samantha\main.cpp|40|error: ambiguous overload for 'operator=' (operand types are 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' and 'bool')|
现在我已经尝试搜索此问题,但找不到任何能帮助我的东西。如果有人可以帮助我确定问题所在以及如何解决该问题,我将不胜感激。
答案 0 :(得分:0)
问题是您正在尝试为字符串分配布尔值。 您有两个选择
false
值input = "false";
请注意,前三个if不会执行任何操作,因为您正在对空字符串文字执行逻辑运算,而不将结果存储在任何地方。
我还建议避免使用if()
条件而不使用花括号,因为这是一种容易出错,难以维护且难以阅读的方法。
else if (input == "yes")
{
booleanVariable = true;
}