为什么使用 and 运算符比较 if 语句中的 2 个对象会抛出错误,而使用 2 if 语句则不会
Why does using the and operator to compare 2 objects in an if statement throw an error when using 2 if statements does not
我在我的磁贴类中是一个方法是TileEqual,我正在尝试将私有变量的形状和颜色与另一个磁贴类进行比较。当我使用 and 运算符 Visual Studio 代码使用当前行时,出现错误"表达式必须是可修改的值">
当我嵌套下面注释掉的 if 语句时,我没有收到任何错误。
bool Tile::isTileEqual(Tile tile) {
Colour colour = tile.getColour();
Shape shape = tile.getShape();
if(this->shape == shape && this->colour = colour) {
//if(this->colour == colour) {
return true;
//}
}
return false;
}
如果 2 个磁贴具有相同的属性,则预期结果将为真。 如果它们不同,则为 false。
在使用&&
的那个上,你只有一个=
。 对于单个=
它是一个作业,您想进行比较。
应该是:
if(this->shape == shape && this->colour == colour) {
我还建议您直接返回表达式的值:
return this->shape == shape && this->colour == colour;
if (x) return true; else return false;
...被认为是丑陋/不良做法。
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 我似乎对if/else的基本语句有问题:/
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果中的多个语句是否与多个 if 相同?
- C++ If/Else 语句被跳过
- 使用语句if时逻辑的问题
- 如何添加语句if-else
- 如何将语句if(isalpha(c))从c++转换为MIPS ?
- 在if语句if C++中创建对象时出现作用域问题
- if语句-if/else和参数放置C++