Shu*_*mar 2 c++ operator-precedence
为什么bitor
与cout
运算符一起使用时不起作用
这有效
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)
根据“ 运算符优先级”,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)