你看到它用于for循环语句,但它在任何地方都是合法的语法.您在其他地方找到了什么用途,如果有的话?
我正在解决有关主教在棋盘上移动的问题。在代码的某一时刻,我有以下语句:
std::cout << (abs(c2-c1) == abs(r2-r1)) ? 1 : 2 << std::endl;
Run Code Online (Sandbox Code Playgroud)
这将产生以下错误:
Run Code Online (Sandbox Code Playgroud)error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'
但是,我通过在代码中包含一个附加变量来立即修复此错误:
error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'
Run Code Online (Sandbox Code Playgroud)
三元运算符如何工作,以及如何确定其返回类型(如编译器称为<unresolved overloaded function type>
)?