c++ - `<<` 和 `==` 之间的运算符优先级问题

标签 c++

#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/

相关文章:

c++ - 如何在C++中找到因子数的位置

c++ - <fstream> 的 ifstream 无法编译

c++ - 为什么 C++ Compare 不需要整数作为其返回值?

c++ - 使用/clr 在 Visual Studio 2010 中 boost

c++ - 如何有效地混合常量和临时返回值?

c++ - 调用左值引用构造函数而不是右值引用构造函数

c++ - 在 C++ 中将 float 转换为 std::string

c++ - 从 cv::connectedComponents 访问标记区域

c++ - 应用程序需要 OS X >= Lion

c++ - WDK C++ 项目需要更改调试编译器严格性