'Call to 'abs' is ambiguous" of integer
#include <bits/stdc++.h>
#include <cstdlib>
using namespace std;
void minimumBribes(vector<int> q) {
bool chaos = false;
int bribes = 0;
for (int i = 0; i<q.size()-1; i++){
if(q[i]-i-1 >= 3 ){
chaos = true;
break;
}
if( (std::abs(q.size()-i - q[q.size()-i-1] )) > 2) continue;
bribes+= std::abs(q.size()-i - q[q.size()-i-1] );
}
if (chaos){
cout<<"Too chaotic"<<endl;
}else{
cout<<bribes<<endl;
}
}
Using C++14, why is it giving me an error "Call to 'abs' is ambiguous"? The input let's say is a vector of integers 1,2,3,5,4. So in the first cycle, it would be abs( 5 - 4 ) and i expect it to be 1,
Solution 1:[1]
Using C++14, why is it giving me an error "Call to 'abs' is ambiguous"?
Because:
- You are calling it with an unsigned type
- There are no overloads for unsigned types
- None of the conversions to the signed types is unambiguously preferred over the others
You can remove the call, because an unsigned type will always be positive, so it makes no sense to call std::abs.
To clarify, the result of the subtraction is unsigned because the left hand operand is an unsigned integer of higher rank than the rank of the signed right hand operand.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
