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

Shu*_*mar 2 c++ operator-precedence

为什么bitorcout运算符一起使用时不起作用

这有效

int a=5,b = 6,d = a bitor b;
cout << d << endl;
Run Code Online (Sandbox Code Playgroud)

这引发错误

int a=5,b=6;
cout << a bitor b << endl;
Run Code Online (Sandbox Code Playgroud)

错误信息:

invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'
  cout << a bitor b << endl;
Run Code Online (Sandbox Code Playgroud)

son*_*yao 5

根据“ 运算符优先级”operator<<具有比更高的优先级operator bitor。然后cout << a bitor b << endl;将被解释为

(cout << a) bitor (b << endl);
Run Code Online (Sandbox Code Playgroud)

b << endl无效。

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

cout << (a bitor b ) << endl;
Run Code Online (Sandbox Code Playgroud)