简体   繁体   中英

C++ trivial function: return value type doesn't match the function return type: ternary operator

I cannot understand why would Visual Studio Intellisense complain. In the code:

int absolute_value(int x) {
    return  x > 0 ?  x : - x;
}

It underlines x in return x with the message: return value type doesn't match the function return type.

My environment is win10, vs2022. I tested the code you posted, the code works fine, I suggest you repair your Visual Studio or download it again.

#include<iostream>
using namespace std;
int absolute_value(int x) {
    return  x > 0 ? x : -x;
}
int main()
{
    cout << absolute_value(-6);
}

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM