如何修复一个“模糊的”函数调用?

时间:2021-08-28 18:15:06

I'm working on a C++ program for class, and my compiler is complaining about an "ambiguous" function call. I suspect that this is because there are several functions defined with different parameters.

我正在为一个类的c++程序工作,我的编译器在抱怨一个“模糊的”函数调用。我猜想这是因为有几个函数定义了不同的参数。

How can I tell the compiler which one I want? Aside from a case-specific fix, is there a general rule, such as typecasting, which might solve these kinds of problems?

我如何告诉编译器我想要哪个?除了一个具体的解决方案之外,还有一个通用的规则,比如类型转换,它可以解决这类问题吗?

Edit:

编辑:

In my case, I tried calling abs() inside of a cout statement, passing in two doubles.

在我的例子中,我尝试在cout语句中调用abs(),传递两个双打。

cout << "Amount is:" << abs(amountOrdered-amountPaid);

cout << Amount is: << abs(amountorde - amountpaid);

Edit2:

Edit2:

I'm including these three headers:

我包括这三个标题:

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

Edit3:

Edit3:

I've finished the program without this code, but in the interest of following through with this question, I've reproduced the problem. The verbatim error is:

没有这段代码,我已经完成了这个程序,但是出于对这个问题的关注,我把这个问题复制了出来。逐字的错误是:

Call to 'abs' is ambiguous.

打电话给“abs”是不明确的。

The compiler offers three versions of abs, each taking a different datatype as a parameter.

编译器提供了三个版本的abs,每个版本都以不同的数据类型作为参数。

4 个解决方案

#1


19  

What's happened is that you've included <cstdlib> (indirectly, since it's included by iostream) along with using namespace std;. This header declares two functions in std with the name abs(). One takes and returns long long, and the other returns long. Plus, there's the one in the global namespace (that returns int) that comes from <stdlib.h>.

所发生的是,您已经将 (因为它包含在iostream中)和使用名称空间std一起包括在内。这个头在std中声明了两个函数,名称为abs()。一种是花很长时间,另一种回报很长。另外,全局命名空间中的一个(返回int)来自于

To fix: well, the abs() that takes double is in <cmath>, and that will actually give you the answer you want!

要解决这个问题:嗯,需要double的abs()在 中,它会给出你想要的答案!

#2


4  

The abs function included by <cstdlib> is overloaded for int and long and long long. Since you give a double as the argument, the compiler does not have an exact fit, so it tries to convert the double to a type that abs accepts, but it does not know if it should try to convert it to int, long, or long long, hence it's ambiguous.

所包含的abs函数为int、long和long的重载。由于您给出了一个double作为参数,所以编译器没有完全匹配,所以它尝试将double转换为abs接受的类型,但是不知道它是否应该尝试将其转换为int、long或long long,因此它是不明确的。

But you probably really want the abs that takes a double and returns a double. For this you need to include <cmath>. Since the double argument matches exactly, the compiler will not complain.

但是你可能真的想要得到双倍回报的腹肌。为此,您需要包括 。由于双参数完全匹配,编译器不会抱怨。

It seems that <cstdlib> gets included automatically when you include the other headers which should not happen. The compiler should have given error: ‘abs’ was not declared in this scope or something similar.

似乎 在包含其他不应该发生的消息头时自动包含在内。编译器应该给出错误:“abs”没有在这个范围或类似的地方声明。

#3


2  

Try using fabs defined in <cmath>. It takes float, double and long double as arguments. abs is defined both in <cmath> and <cstdlib>. The difference is abs(int), abs(long) and abs(long long) are defined in <cstdlib> while other versions are defined in <cmath>.

尝试使用在 中定义的fabs。它需要float, double和long double作为参数。abs在 中都有定义。不同的是abs(int), abs(long)和abs(long long)在 中定义,而其他版本在 中定义。

#4


-2  

Not sure why this isn't calling the int version of abs but you could try type casting the expression (amountOrdered - amountPaid) as int i.e.

不知道为什么这不是调用int版本的abs,但是您可以尝试将表达式(amountOrdered - amountPaid)作为int类型来表示。

cout <<"Amount is: "<< abs( (int)(amountOrdered - amountPaint) );

#1


19  

What's happened is that you've included <cstdlib> (indirectly, since it's included by iostream) along with using namespace std;. This header declares two functions in std with the name abs(). One takes and returns long long, and the other returns long. Plus, there's the one in the global namespace (that returns int) that comes from <stdlib.h>.

所发生的是,您已经将 (因为它包含在iostream中)和使用名称空间std一起包括在内。这个头在std中声明了两个函数,名称为abs()。一种是花很长时间,另一种回报很长。另外,全局命名空间中的一个(返回int)来自于

To fix: well, the abs() that takes double is in <cmath>, and that will actually give you the answer you want!

要解决这个问题:嗯,需要double的abs()在 中,它会给出你想要的答案!

#2


4  

The abs function included by <cstdlib> is overloaded for int and long and long long. Since you give a double as the argument, the compiler does not have an exact fit, so it tries to convert the double to a type that abs accepts, but it does not know if it should try to convert it to int, long, or long long, hence it's ambiguous.

所包含的abs函数为int、long和long的重载。由于您给出了一个double作为参数,所以编译器没有完全匹配,所以它尝试将double转换为abs接受的类型,但是不知道它是否应该尝试将其转换为int、long或long long,因此它是不明确的。

But you probably really want the abs that takes a double and returns a double. For this you need to include <cmath>. Since the double argument matches exactly, the compiler will not complain.

但是你可能真的想要得到双倍回报的腹肌。为此,您需要包括 。由于双参数完全匹配,编译器不会抱怨。

It seems that <cstdlib> gets included automatically when you include the other headers which should not happen. The compiler should have given error: ‘abs’ was not declared in this scope or something similar.

似乎 在包含其他不应该发生的消息头时自动包含在内。编译器应该给出错误:“abs”没有在这个范围或类似的地方声明。

#3


2  

Try using fabs defined in <cmath>. It takes float, double and long double as arguments. abs is defined both in <cmath> and <cstdlib>. The difference is abs(int), abs(long) and abs(long long) are defined in <cstdlib> while other versions are defined in <cmath>.

尝试使用在 中定义的fabs。它需要float, double和long double作为参数。abs在 中都有定义。不同的是abs(int), abs(long)和abs(long long)在 中定义,而其他版本在 中定义。

#4


-2  

Not sure why this isn't calling the int version of abs but you could try type casting the expression (amountOrdered - amountPaid) as int i.e.

不知道为什么这不是调用int版本的abs,但是您可以尝试将表达式(amountOrdered - amountPaid)作为int类型来表示。

cout <<"Amount is: "<< abs( (int)(amountOrdered - amountPaint) );