0
#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,

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Don't describe the error, paste it verbatim. See also https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h. – Passer By Mar 31 '22 at 13:27
  • The issue is, that std::abs has multiple definitions, according to https://en.cppreference.com/w/cpp/numeric/math/abs. 'xxx is ambiguous' usually means, that the compiler can't figure out which of the definitions it should use. To maybe remedy the issue, try storing the result of your calculation in an integer and run that variable through std::abs. – Refugnic Eternium Mar 31 '22 at 13:27
  • 2
    MCVE : https://godbolt.org/z/Ecx9ncrGa – François Andrieux Mar 31 '22 at 13:28
  • 1
    [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Evg Mar 31 '22 at 13:29
  • 3
    It looks like the problem is `abs` only uses `signed` types but you are passing a `unsigned` type. This means the function won't work as expected, if you end up storing a negative result in an `unsigned` integer you get a large positive value instead. `abs` would always just return its argument. – François Andrieux Mar 31 '22 at 13:29
  • Relevant: [g++ error: call of overloaded 'abs(unsigned int)' is ambiguous](https://stackoverflow.com/q/50301469/580083). Note that it does not make sense to call `abs` with an unsinged argument. – Daniel Langr Mar 31 '22 at 13:30
  • @zkoza There are many kinder ways than your sarcastic comment to ask for more easily-readable indentation. I know that we are not always at our best. I can be quite terse sometimes as well. – JohnFilleau Mar 31 '22 at 13:55

1 Answers1

3

Using C++14, why is it giving me an error "Call to 'abs' is ambiguous"?

Because:

  1. You are calling it with an unsigned type
  2. There are no overloads for unsigned types
  3. 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.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 2
    Maybe a better suggestion would be to calculate the argument of `abs` in signed arithmetic. It seems to be what OP wants (IMO). – Daniel Langr Mar 31 '22 at 13:36
  • @DanielLangr Depends on whether it is supposed to be signed or unsigned. OP will have to decide what they want to do. – eerorika Mar 31 '22 at 13:50