g++ error: call of overloaded 'abs(unsigned int)' is ambiguous

g++ error: call of overloaded 'abs(unsigned int)' is ambiguous

我正在尝试编译以下代码:

#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;
}

使用 g++ test.cc -o test.

但是,g++ 编译器失败并出现以下错误:

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); }                                                
   ^~~     

为什么会出现此错误以及如何解决?

g++版本为:gcc版本6.3.0

编辑:<cstdlib> 应该处理重载(无论如何应该是首选),参见问题 here

注意这里有一些逻辑错误。首先,你正在做的可能不是你想要的。 res1 - res2 是两个 unsigned 类型之间的算术运算,因此结果也将是 unsigned。如果低于 0,则返回到 max int。 abs() 在这里毫无意义,该值将 永远不会为负数 因为类型不允许它。即使使用编译此代码的 header,编译器仍会对此发出警告。

我强烈建议您在处理算术时坚持使用有符号整数以避免这些陷阱,如果您确实需要存储空间,请使用 64 位整数。

所以我真正的答案是,将 unsigned 切换为 int

另请注意:unsigned res2 = i / res1; res2 将被截断为 0,我不确定这是否是您想要的。

您可以将结果转换为带符号的数据类型,以便 abs 知道您调用的是哪个数据类型:

while( abs( int(res1 - res2) ) > 1 )

但是重新考虑一下你在这里需要做什么,因为对无符号的减法运算的结果仍然是无符号的,所以如果结果低于 0 它将变成一个很大的数字(作为正常溢出),所以我认为首先使用带符号变量是解决问题的最佳方法

int iSqr( unsigned i )
{
    int res1 = 2;
    int res2 = i/res1;
    while( abs(res1 - res2) > 1 )
    {
        res1 = (res1 + res2)/2;
        res2 = i/res1;
    }
    return res1 < res2 ? res1 : res2;
}

abs<stdlib.h> 中(自 c++11 起):http://www.cplusplus.com/reference/cstdlib/abs/

          int abs (          int n);
     long int abs (     long int n);
long long int abs (long long int n);

如果过载,调用将不明确 resolution 无法 select 匹配到比此类不可微函数更好的调用。

您可以像这样显式转换参数:

static_cast<int>(res1 - res2)