C++ 如何修复“模棱两可”的函数调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7549874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:06:39  来源:igfitidea点击:

How do I fix an "ambiguous" function call?

c++ambiguous-call

提问by Moshe

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 coutstatement, passing in two doubles.

就我而言,我尝试abs()cout语句内部调用,传入两个doubles。

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

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

Edit2:

编辑2:

I'm including these three headers:

我包括这三个标题:

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

using namespace std;

Edit3:

编辑3:

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,每个版本都采用不同的数据类型作为参数。

回答by mkb

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 stdwith 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>.

什么事是你已经包括<cstdlib>(间接的,因为它是由包括iostream)沿using namespace std;。这个头文件std用名称声明了两个函数abs()。一个接受并返回long long,另一个返回long。另外,全局命名空间(返回int)中有一个来自<stdlib.h>.

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

修复:嗯,abs()需要 double 的 in <cmath>,实际上会给你你想要的答案!

回答by JohnPS

The absfunction included by <cstdlib>is overloaded for intand longand long long. Since you give a doubleas the argument, the compiler does not have an exact fit, so it tries to convert the doubleto a type that absaccepts, but it does not know if it should try to convert it to int, long, or long long, hence it's ambiguous.

abs由包括功能<cstdlib>被重载intlonglong long。由于您给出 adouble作为参数,编译器没有完全匹配,因此它尝试将 the 转换为接受double的类型abs,但它不知道是否应该尝试将其转换为int, long, or long long,因此它是模棱两可的。

But you probably really want the absthat takes a doubleand returns a double. For this you need to include <cmath>. Since the doubleargument matches exactly, the compiler will not complain.

但是您可能真的想要abs接受 adouble并返回 a 的double。为此,您需要包含<cmath>. 由于double参数完全匹配,编译器不会抱怨。

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 scopeor something similar.

<cstdlib>当您包含不应发生的其他标题时,似乎会自动包含在内。编译器应该已经给出error: ‘abs' was not declared in this scope或类似的东西。

回答by fr0stw01f

Try using fabsdefined in <cmath>. It takes float, doubleand long doubleas arguments. absis 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定义的<cmath>。它需要float,doublelong double作为参数。abs<cmath>和 中都定义了<cstdlib>。所不同的是abs(int)abs(long)abs(long long)中定义<cstdlib>,而其他版本中定义<cmath>

回答by Vineet G

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.

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

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