布尔类型操作
boolean type manipulation
此代码
#include <iostream>
using namespace std;
int main(){
bool t=false;
cout<<t &&(!t)<<endl;
return 0;
}
显示如下错误
类型为"bool"answers"binary"的无效操作数"operator<& lt;"
怎么了?这个我不明白,请给我解释一下。我认为&&
和!
是在c++中定义的。
怎么了?
"类型'bool'和'到二进制'操作符<<'的无效操作数"
这意味着第二个<<
操作符正在尝试对(!t)和'endl'执行。
<<
具有比&&
更高的优先级,因此您的cout语句执行如下:
(cout << t ) && ( (!t) << endl );
添加括号修复:
cout << (t && (!t) ) << endl ;
添加圆括号,使操作符的优先级正确:
cout << (t && !t) << endl;
等同于:
cout << false << endl;
&&
的优先级低于<<
,因此语句被计算为(cout << t) && (!t << endl);
你需要更多的括号:
cout << (t && !t) << endl;
问题在于运算符优先级,因为 &&
的优先级低于<<
。
cout<<(t && (!t))<<endl; // ok!
同样,对于任何bool
变量t
,表达式t && (!t)
总是得到false
, t || (!t)
总是得到true
。:)
相关文章:
- 逐位操作的隐式类型转换
- 像union_这样的 Boost.Geometry 操作如何处理浮点类型的基本不精确性?
- 为什么 std::lerp 不适用于任何已实现所需操作的类型?
- C++:将值赋值给原始数据类型(例如布尔值)是原子操作吗?
- 标量类型的特征模板无法编译固定大小的子矩阵操作
- 为表示一个或多个操作的C++函数的int参数寻找类型安全的替换
- 对于 CPU 无法原子操作的类型,std::atomic 有什么意义?
- "+=" 操作在类型之间不起作用 std::复杂<double>和__complex__双精度
- 使用用户定义的操作隐式转换为类型
- 元数据操作失败LNK2022错误 (8013118D):重复类型中的布局信息不一致 (选择设备参数):(0x020002
- 是否有可能具有放入容器的移动操作的类型?
- 在 Fortran 中泛化特定声明类型的操作
- 为什么对小于 4 个字节的整数类型的位操作会发生意外行为?
- 对标准类型使用原子操作
- POD 类型的原子按位操作
- CAN模板类型推理考虑原始操作类型转换
- 是浮点操作,导致IEC 559/IEEE 754浮点类类型的无限行为
- STD ::向量迭代器类型和允许的操作
- 从网络应用程序拆包操作类型的正确方式
- 映射平台特定的可互操作类型