C++ 在 cout 中使用关系表达式

发布时间 2023-05-31 20:28:15作者: AibaAsagi

用std::cout输出关系运算表达式时,关系表达式要加括号,否则编译会报错。例如:

#include <iostream>

int main(int argc, char **argv) {
    std::cout << 1 < 2 << std::endl;
    return 0;
}

在 linux 中编译后报错内容如下:

test.cpp: In function 'int main(int, char**)':
test.cpp:4:24: error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'
    4 |     std::cout << 1 < 2 << std::endl;
      |