在 c++ 中使用重载的歧义错误
Ambiguity error using overload in c++
我尝试使用带有浮点数和整数的重载。当我只使用整数时,代码工作正常,但是当我包含浮点数时,它给了我错误。代码如下:
#include <iostream>
using namespace std;
int calculate(int x,int y);
float calculate(float x,float y);
const int MAININT=4;
int main()
{
int result=calculate(5,5);
float fresult=calculate(7.5,7.5); LINE X
cout << (result + MAININT + fresult); LINE Y
return 0;
}
int calculate(int x,int y)
{
int result=x*y;
return result;
}
float calculate(float x,float y)
{
int result=x*y;
return result;
}
通过删除 X 行并从 Y 行中删除结果,代码没有给我任何错误。所以我认为 LINE X 中一定有问题,但我不明白为什么我会收到错误。
我收到的错误消息是:
[Error] call of overloaded 'calculate(double, double)' is ambiguous
[Note] candidates are:
[Note] int calculate(int, int)
[Note] float calculate(float, float)
我不理解错误消息,所以我没有包含它们。我从松元瑶的回答中明白我做错了什么,但下次我会从一开始就在我的问题中包含错误消息,这样在代码中更容易看到我做错了什么。
因为7.5
是一个double
(参见浮点文字),而不是一个float
;隐式转换为int
或float
被认为是相同的排名。
如果你的假设7.5
在这里float
,你可以使用后缀f
或F
使其成为float
文字。
float fresult = calculate(7.5f, 7.5f); // 7.5f is a float literal; no ambiguity
或者使用显式转换:
float fresult = calculate(static_cast<float>(7.5), static_cast<float>(7.5));
您应该已经发布了错误消息,该错误消息本身是不言自明的。错误消息提到了候选函数以及它们如何不完全兼容:
error: call of overloaded 'calculate(double, double)' is ambiguous
float fresult=calculate(7.5,7.5);
^
note: candidate: int calculate(int, int)
int calculate(int x,int y);
^
note: candidate: float calculate(float, float)
float calculate(float x,float y);
默认情况下,浮点文本(在您的情况下7.5
)的类型为 double。
下面是确定浮点文本类型的后缀列表:
- (无后缀)定义
double
-
f
F
定义float
-
l
L
定义long double
虽然其他人已经告诉你歧义错误来自哪里,但我很惊讶没有人提到这个问题的最简单解决方案:只需使用double
而不是float
。
就像int
应该是整数的默认选择一样,double
应该是浮点数的默认选择。 float
适用于非常特殊的用例,这些用例都可能不适用于您的情况。请参阅"何时使用float,何时使用double",请参阅Software Engineering Stack Exchange。
作为遵循本指南的副作用,您在此处的特定问题将消失:
#include <iostream>
using namespace std;
int calculate(int x,int y);
double calculate(double x, double y);
const int MAININT=4;
int main()
{
int result=calculate(5,5);
double fresult=calculate(7.5,7.5);
cout << (result + MAININT + fresult);
return 0;
}
int calculate(int x,int y)
{
int result=x*y;
return result;
}
double calculate(double x, double y)
{
double result=x*y; // the `int` here was a mistake in your original code anyway
return result;
}
一些进一步的建议:
- 避免
using namespace std;
。 - 为预处理器宏保留
ALL_CAPS
。
相关文章:
- C++17 中的歧义错误(模板模板参数和默认参数问题)
- 运算符的歧义错误<<自定义 std::ostream 子类中的重载
- 了解函数错误的歧义新声明
- 在C 中,静态成员函数是否继承了?如果是,为什么没有出现歧义错误
- 在 c++ 中使用重载的歧义错误
- 转换 - 错误 E2015,AnsiString(char) 和 AnsiString(short) 之间的歧义
- C++代码中出现歧义错误
- 可变模板和混合的歧义错误
- 错误:"std::copy "之间的重载歧义
- c++操作符string和char*导致歧义错误
- 使用c++ Builder 10 Seattle有很多歧义错误
- c++歧义符号错误
- 函数重载时出现歧义错误
- c++矢量错误,显示变量有歧义-我已经尝试了一切仍然没有希望
- 为什么这两个构造函数一起不会产生歧义错误?
- C2870符号歧义错误
- 由于模板函数声明中存在歧义而导致错误
- 与第三方头文件相关的C++Builder歧义错误
- 模板类相互使用会产生歧义错误
- 错误:对旧声明“double-round(double)”存在歧义