相关疑难解决方法(0)

为什么在条件运算符(?:)中,第二个和第三个操作数必须具有相同的类型?

为什么在条件运算符(?:),第二和第三个操作数必须具有相同的类型?

我的代码是这样的:

#include <iostream>
using std::cout;

int main()
{
    int a=2, b=3;
    cout << ( a>b ? "a is greater\n" : b );  /* expression ONE */
    a>b? "a is greater\n" : b;               /* expression TWO */

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用g ++编译时,会发出错误:

main.cpp:7:36: error: operands to ?: have different types ‘const char*’ and ‘int’
main.cpp:8:28: error: operands to ?: have different types ‘const char*’ and ‘int’
Run Code Online (Sandbox Code Playgroud)

我想知道为什么他们必须有相同的类型?

(1)在我看来,如果(a>b)是,那么表达式( a>b ? "a is …

c c++ operator-keyword

16
推荐指数
3
解决办法
7296
查看次数

标签 统计

c ×1

c++ ×1

operator-keyword ×1