Marcelo Casanova
Marcelo Casanova

Reputation: 21

When using abs() it shows "abs is ambiguous" but when I try it on another editor it works

In my code abs() isnt working but when put in a different code editor it does work. I dont know what to do. Cant find answers anywhere. Please help. Error: ("abs" is ambiguousC/C++(266))

#include <iostream>
#include <cstdlib>

using namespace std;

int main(){

    int x = -1;
    int a = abs(x);

    cout<< a;

}

Upvotes: 2

Views: 2052

Answers (3)

Chucky
Chucky

Reputation: 543

As commented earlier under the question, the error is not related to abs() itself: it's a code quality issue detected by the C/C++ extension by Microsoft for the Visual Studio Code editor which is somewhat inappropriate.

Workaround:

[The] error description go away if I either a) make the namespace explicit at the site of the supposed error (not that this is always desirable) or b) edit the definition and declaration to make them temporarily invalid and then edit them back (not that this leaves me confident that the squiggle won't come back).

Source: https://developercommunity.visualstudio.com/t/showing-error-is-ambiguous-even-though-the-code-co/405607

Upvotes: 0

harkhuang
harkhuang

Reputation: 30

#include <iostream>
#include <cstdlib>

using namespace std;

int main(){

    int x = -1;
    int a = fabs(x);

    cout<< a;

}

or


#include <iostream>
#include <stdlib.h>


int main() {

    int x = -1;
    int a = abs(x);

    std::cout << a;

}

Upvotes: 0

Paul Wang
Paul Wang

Reputation: 97

More information will be helpful. There's no need to add "using namespace std" here. You can try to modify "int a = abs(x);" ==> "int a = ::abs(x);"

Upvotes: 1

Related Questions