如何解决“模棱两可”的问题?函数调用?
我正在为类开发一个 C++ 程序,我的编译器抱怨“不明确”的函数调用。我怀疑这是因为有几个函数定义了不同的参数。
我如何告诉编译器我想要哪一个?除了特定情况的修复之外,是否有通用规则(例如类型转换)可以解决此类问题?
编辑:
就我而言,我尝试在cout
语句内调用abs()
,并传入两个double
s。
<代码>cout << “金额为:”<< abs(amountOrdered-amountPaid);
Edit2:
我包括这三个标头:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
Edit3:
我已经完成了没有此代码的程序,但在为了解决这个问题,我重现了这个问题。逐字错误是:
对“abs”的调用不明确。
编译器提供了三个版本的abs,每个版本都采用不同的数据类型作为参数。
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.
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 double
s.
cout << "Amount is:" << abs(amountOrdered-amountPaid);
Edit2:
I'm including these three headers:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
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.
The compiler offers three versions of abs
, each taking a different datatype as a parameter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
发生的情况是,您已经包含了
(间接包含,因为它是由iostream
包含的)以及using namespace std;
。此标头在std
中声明了两个名为abs()
的函数。一个获取并返回long long
,另一个返回long
。另外,全局命名空间中还有一个来自
的命名空间(返回int
)。修复:嗯,采用 double 的
abs()
位于
中,这实际上会给您想要的答案!What's happened is that you've included
<cstdlib>
(indirectly, since it's included byiostream
) along withusing namespace std;
. This header declares two functions instd
with the nameabs()
. One takes and returnslong long
, and the other returnslong
. Plus, there's the one in the global namespace (that returnsint
) that comes from<stdlib.h>
.To fix: well, the
abs()
that takes double is in<cmath>
, and that will actually give you the answer you want!
包含的abs
函数针对int
和long
以及long long< 进行了重载/代码>。由于您提供了
double
作为参数,编译器没有精确的拟合,因此它尝试将double
转换为abs
的类型> 接受,但它不知道是否应该尝试将其转换为int
、long
或long long
,因此它是不明确的。但您可能确实想要接受
double
并返回double
的abs
。为此,您需要包含
。由于 double 参数完全匹配,编译器不会抱怨。当您包含其他不应发生的标头时,似乎
会自动包含在内。编译器应该给出错误:'abs'未在此范围内声明
或类似的信息。The
abs
function included by<cstdlib>
is overloaded forint
andlong
andlong long
. Since you give adouble
as the argument, the compiler does not have an exact fit, so it tries to convert thedouble
to a type thatabs
accepts, but it does not know if it should try to convert it toint
,long
, orlong long
, hence it's ambiguous.But you probably really want the
abs
that takes adouble
and returns adouble
. For this you need to include<cmath>
. Since thedouble
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 givenerror: ‘abs’ was not declared in this scope
or something similar.尝试使用< /code> 而其他版本在
中定义的fabs
。它接受float
、double
和long double
作为参数。abs
在
和
中定义。区别在于abs(int)
、abs(long)
和abs(long long)
定义于
中定义。Try using
fabs
defined in<cmath>
. It takesfloat
,double
andlong double
as arguments.abs
is defined both in<cmath>
and<cstdlib>
. The difference isabs(int)
,abs(long)
andabs(long long)
are defined in<cstdlib>
while other versions are defined in<cmath>
.不知道为什么这没有调用 int 版本的 abs,但您可以尝试将表达式 (amountOrdered - amountPaid) 类型转换为 int ie
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.