I am trying to compile the following code:
#include <stdlib.h>
static
unsigned iSqr( unsigned i )
{
unsigned res1 = 2;
unsigned res2 = i/res1;
while( abs( res1 - res2 ) > 1 )
{
res1 = (res1 + res2)/2;
res2 = i/res1;
}
return res1 < res2 ? res1 : res2;
}
using g++ test.cc -o test
.
However, g++ compiler fails with the following error:
test.cc: In function 'unsigned int iSqr(unsigned int)':
test.cc:8:29: error: call of overloaded 'abs(unsigned int)' is ambiguous
while( abs( res1 - res2 ) > 1 )
^
In file included from /usr/include/c++/6/cstdlib:75:0,
from /usr/include/c++/6/stdlib.h:36,
from test.cc:2:
/usr/include/stdlib.h:735:12: note: candidate: int abs(int)
extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
^~~
In file included from /usr/include/c++/6/stdlib.h:36:0,
from test.cc:2:
/usr/include/c++/6/cstdlib:185:3: note: candidate: __int128 std::abs(__int128)
abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
^~~
/usr/include/c++/6/cstdlib:180:3: note: candidate: long long int std::abs(long long int)
abs(long long __x) { return __builtin_llabs (__x); }
^~~
/usr/include/c++/6/cstdlib:172:3: note: candidate: long int std::abs(long int)
abs(long __i) { return __builtin_labs(__i); }
^~~
Why this error is happening and how to fix it?
g++ version is: gcc version 6.3.0