使用float给出“对重载函数的调用是不明确的”错误 [英] Using float gives "call to overloaded function is ambiguous" error
问题描述
我重载函数 add()
,但是当我使用 float
数据类型时,错误。然而,当我把它改为 double
,那么它的工作正常。为什么 float
导致错误?
代码是:
#include< iostream>
using namespace std;
class students {
private:
int i;
float f
public:
void add(int b){
i = b;
cout<< First Int:<<一世;
}
void add(float c){
f = c;
cout<< Second Int:<< F;
}
};
int main(){
students obj;
obj.add(9);
obj.add(5.5);
}
错误:
在函数'int main()':
[错误]调用重载的'add(double)'是不明确的
[注意]候选是:
[注意] void students :: add(int)
[注意] void students :: add(float)
是 double
,但是没有一个你的函数需要一个 double
参数。因此,编译器对是否使用 int
参数或者使用 float
参数调用函数感到困惑。
这就是为什么当你改变函数有一个 double
参数,错误不再来了,因为现在有一个函数可以接受 double
参数,因此有歧义。
您也可以通过调用
obj.add ;
在数字后添加 f
§2.13.4
1浮动文本由整数部分,小数点,
小数部分,e或E,一个可选的有符号整数指数,以及
一个可选的类型后缀。整数和小数部分都由十进制(十进制)数字序列的
组成。当确定其值时,将忽略数字序列中的可选分离单个
引号。 [
示例:文本1.602'176'565e-19和1.602176565e-19具有
相同的值。 -end example]整数部分或分数
部分(不是两者)都可以省略;可以省略小数点或字母
e(或E)和指数(不是两个)。整数
部分,可选的小数点和可选的分数部分形式
浮动文本的有效部分。指数,如果
存在,指示10的幂,其中有效部分是
被缩放。如果缩放值在其类型的可表示值
的范围内,则结果是可表示的缩放值,否则
最接近缩放值的更大或更小的可表示值,
实现定义的方式。 浮动
文字的类型是double,除非由后缀明确指定。
后缀f和F指定float,后缀l和L指定long
double。如果缩放值不在其类型的可表示
值的范围内,
(对不起,发布所有的,但你可以了解更多关于 float
以这种方式)
I'm overloading the function add()
, but when I used the float
datatype it is showing an error. However, when I change it to double
, then it's working fine. Why is float
causing the error?
Code is:
#include <iostream>
using namespace std;
class students{
private:
int i;
float f;
public:
void add(int b){
i=b;
cout << "First Int: " << i;
}
void add(float c){
f=c;
cout << "Second Int: " << f;
}
};
int main(){
students obj;
obj.add(9);
obj.add(5.5);
}
Errors:
In function 'int main()':
[Error] call of overloaded 'add(double)' is ambiguous
[Note] candidates are:
[Note] void students::add(int)
[Note] void students::add(float)
解决方案 5.5
is a double
, but none of your functions take a double
argument. So, the compiler gets confused on whether to call the function with the int
parameter, or the function with the float
parameter. So, you get a an error saying it is ambiguous.
That is why when you changed the function to have a double
parameter, the error no longer came, because now there is a function which can take a double
argument, and thus there is ambiguity there.
You can also fix the problem by calling the function as
obj.add(5.5f);
Adding the f
after a number makes it to a float.
Let's look at the C++ Standard
§ 2.13.4
1 A floating literal consists of an integer part, a decimal point, a
fraction part, an e or E, an optionally signed integer exponent, and
an optional type suffix. The integer and fraction parts both consist
of a sequence of decimal (base ten) digits. Optional separating single
quotes in a digit-sequence are ignored when determining its value. [
Example: The literals 1.602’176’565e-19 and 1.602176565e-19 have the
same value. —end example ] Either the integer part or the fraction
part (not both) can be omitted; either the decimal point or the letter
e (or E ) and the exponent (not both) can be omitted. The integer
part, the optional decimal point and the optional fraction part form
the significant part of the floating literal. The exponent, if
present, indicates the power of 10 by which the significant part is to
be scaled. If the scaled value is in the range of representable values
for its type, the result is the scaled value if representable, else
the larger or smaller representable value nearest the scaled value,
chosen in an implementation-defined manner. The type of a floating
literal is double unless explicitly specified by a suffix. The
suffixes f and F specify float, the suffixes l and L specify long
double. If the scaled value is not in the range of representable
values for its type, the program is ill-formed.
( Sorry for posting all of it, but you can learn more about float
s this way )
这篇关于使用float给出“对重载函数的调用是不明确的”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!