How to fix "error: call to 'abs' is ambiguous"

How to fix "error: call to 'abs' is ambiguous"

我正在 运行从 HackerRank 获取一个关于指针的简单 C++ 程序,它在网站上运行良好。然而, 当我在 MacOS 上 运行 它时,我得到 error: call to 'abs' is ambiguous 并且我不确定到底什么是模棱两可的。

我查看了类似问题的其他答案,但错误消息往往是 Ambiguous overload call to abs(double),这不是我遇到的问题,因为我没有使用任何双打。我也试过包含头文件 cmathmath.h,但问题仍然存在。

#include <stdio.h>
#include <cmath>

void update(int *a,int *b) {
    int num1 = *a;
    int num2 = *b;
    *a = num1 + num2;
    *b = abs(num1 - num2);
}

int main() {
    int a, b;
    int *pa = &a, *pb = &b;

    scanf("%d %d", &a, &b);
    update(pa, pb);
    printf("%d\n%d", a, b);

    return 0;
}

我的问题出现在第 8 行。

完整的错误信息是:

$ clang++ test.cpp
test.cpp:8:10: error: call to 'abs' is ambiguous
    *b = abs(num1 - num2);
         ^~~
.../include/c++/v1/math.h:769:1: note: candidate function
abs(float __lcpp_x) _NOEXCEPT {return ::fabsf(__lcpp_x);}
^
.../include/c++/v1/math.h:769:1: note: candidate function
abs(double __lcpp_x) _NOEXCEPT {return ::fabs(__lcpp_x);}
^
.../include/c++/v1/math.h:769:1: note: candidate function
abs(long double __lcpp_x) _NOEXCEPT {return ::fabsl(__lcpp_x);}
^
1 error generated.

您从 <cmath> 获得的 abs 的三个重载是 abs(float)abs(double)abs(long double);这是不明确的,因为您有一个 int 参数,而编译器不知道要转换为哪个 floating-point 类型。

abs(int)<cstdlib> 中定义,因此 #include <cstdlib> 将解决您的问题。

如果您使用的是 Xcode,您可以在问题导航器 (⌘5) 中获取有关错误的更多详细信息,然后单击问题旁边的三角形。

如果您使用的是 C 编译器,则应包括

#include <stdlib.h>

并使用不带 std:: 的 abs。 如果您使用 C++ 编译器,那么您应该将 abs 更改为 std::abs.

希望对您有所帮助:)

对我来说,#include <cstdlib> 没有解决问题,可能是因为我不需要包含任何东西就可以使用 abs。因此,如果它通过显式转换帮助其他人,它对我来说效果很好,就像在下一个代码中一样:

*b = abs(int(num1 - num2));

在模板代码中,很容易忽略std::abs没有为无符号类型定义。例如,如果为无符号类型实例化以下方法,编译器可能会合理地抱怨 std::abs 未定义:

template<typename T>
bool areClose(const T& left, const T& right) {
    // This is bad because for unsigned T, std::abs is undefined
    // and for integral T, we compare with a float instead of
    // comparing for equality:
    return (std::abs(left - right) < 1e-7);
}

int main() {
    uint32_t vLeft = 17;
    uint32_t vRight = 18;
    std::cout << "Are the values close? " << areClose(vLeft, vRight) << std::endl;
}

上面代码中 areClose() 的更好定义,巧合的是也解决了 std::abs() 未定义的问题,可能看起来像这样:

template<typename T>
bool areClose(const T& left, const T& right) {
    // This is better: compare all integral values for equality:
    if constexpr (std::is_integral<T>::value) {
        return (left == right);
    } else {
        return (std::abs(left - right) < 1e-7);
    }
}

我使用 #include <bits/stdc++.h> 作为唯一的包含语句,它对我有用。 我的代码:

#include <bits/stdc++.h>  
using namespace std;
class Solution {
public:
    vector<int> findDuplicates(vector<int>& nums) {
        int n = nums.size();
        if(n == 0 || n == 1)
            return {};
        vector<int> ans;
        for(int i = 0; i < n; i++)
        {
            if(nums[abs(nums[i])-1] < 0)
                ans.push_back(abs(nums[i]));
            else
                nums[abs(nums[i])-1] = -1 * nums[abs(nums[i])-1];
        }
        return ans;
    }
};