#include <bits/stdc++.h>
using namespace std;
class point
{
public:
int x, y;
point(int a, int b)
{
x = a, y = b;
}
bool operator==(point &a)
{
if (a.x == x && a.y == y)
{
return 1;
}
else
{
return 0;
}
}
};
int main()
{
point a(5, 7), b(5, 7);
int t = a == b;
cout<<t; //it is working properly
cout<< (a==b); // it also
cout << a==b; //but it gives me compilation error
}
是什么原因...??
是否有任何运算符优先级的事实......?
我不知道为什么它会给出编译错误...
正如所见,它看起来不错
最佳答案
<<
运算符的优先级高于 ==
运算符(operator)。所以这:
cout << a==b;
解析如下:
(cout << a ) == b;
这会导致错误,因为没有
operator<<
的过载为 cout
需要 point
作为论据。第一个示例中的显式括号是处理此问题的正确方法。
关于c++ - `<<` 和 `==` 之间的运算符优先级问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61645640/