如何解决“模棱两可”的问题?函数调用?

发布于 2024-12-06 05:32:29 字数 670 浏览 2 评论 0原文

我正在为类开发一个 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 doubles.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉月流沐 2024-12-13 05:32:29

发生的情况是,您已经包含了 (间接包含,因为它是由 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 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!

尤怨 2024-12-13 05:32:29

包含的 abs 函数针对 intlong 以及 long long< 进行了重载/代码>。由于您提供了 double 作为参数,编译器没有精确的拟合,因此它尝试将 double 转换为 abs 的类型> 接受,但它不知道是否应该尝试将其转换为 intlonglong long,因此它是不明确的。

但您可能确实想要接受 double 并返回 doubleabs。为此,您需要包含 。由于 double 参数完全匹配,编译器不会抱怨。

当您包含其他不应发生的标头时,似乎 会自动包含在内。编译器应该给出错误:'abs'未在此范围内声明或类似的信息。

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.

最偏执的依靠 2024-12-13 05:32:29

尝试使用 中定义的 fabs。它接受 floatdoublelong double 作为参数。 abs 中定义。区别在于 abs(int)abs(long)abs(long long) 定义于 < /code> 而其他版本在 中定义。

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

浅听莫相离 2024-12-13 05:32:29

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

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

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.

cout <<"Amount is: "<< abs( (int)(amountOrdered - amountPaint) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文