Reputation: 21
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
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).
Upvotes: 0
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
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