Alex S.
Alex S.

Reputation: 1214

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.

Upvotes: 0

Views: 104

Answers (1)

Yujian Yao - MSFT
Yujian Yao - MSFT

Reputation: 954

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);
}

enter image description here

Upvotes: 1

Related Questions