将逻辑 OR 与 cout 运算符一起使用

Using logical OR with cout operator

本文关键字:运算符 一起 cout OR      更新时间:2023-10-16

为什么bitor在与运算符一起使用时不起作用cout

这行得通

int a=5,b = 6,d = a bitor b;
cout << d << endl;

这是抛出错误

int a=5,b=6;
cout << a bitor b << endl;

错误信息:

invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'
cout << a bitor b << endl;

根据运算符优先级,operator<<的优先级高于operator bitor。然后cout << a bitor b << endl;将被解释为

(cout << a) bitor (b << endl);

b << endl无效。

您可以添加括号以指定正确的优先级,例如

cout << (a bitor b ) << endl;