2012-12-04 26 views
0

我有两个整数并试图将它们传递给cout无法将操作'&'结果传递给cout

int a =1; 
int b= 3; 
cout<<a&b; 

编译器会告诉:

Error 2 error C2676: binary '&' : 'std::basic_ostream<_Elem,_Traits>' does not define this operator or a conversion to a type acceptable to the predefined operator 

但& b返回int这是可以理解的 '< <' 操作。

为什么这个错误会上升?

回答

1

由于运营商的优先级,你需要使用括号:

cout << (a & b) 

<<操作的优先级比&越紧,所以省略了括号,使 编译undertand它作为(cout << a) & b,这也解释了错误报告: &运算符不能用于流(返回的对象从cout << a)和int。

1

这是一个优先问题,如果我没有弄错的话。尝试使用cout << (a&b);,看看它是否没有更好的工作。

+0

就是这样 –

1

你可以这样做:)或我误解? (a)

int a =1; 
int b= 3; 
cout<<a << "&" << b;