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 double
s.
在我的例子中,我尝试在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>
.
所发生的是,您已经将
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.
由
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.
似乎
#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>
.
尝试使用在
#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>
.
所发生的是,您已经将
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.
由
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.
似乎
#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>
.
尝试使用在
#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) );