使用 float 会产生 "call to overloaded function is ambiguous" 错误
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: " << i;
}
void add(float c){
f=c;
cout << "Second Int: " << f;
}
};
int main(){
students obj;
obj.add(9);
obj.add(5.5);
}
错误:
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
是 double
,但 none 的函数采用 double
参数。因此,编译器会混淆是调用带有 int
参数的函数,还是调用带有 float
参数的函数。所以,你会收到一条错误消息,说它不明确。
这就是为什么当您将函数更改为具有 double
参数时,错误不再出现,因为现在有一个函数可以接受 double
参数,因此有那里有歧义。
您也可以通过调用函数 as
来解决问题
obj.add(5.5f);
在数字后添加 f
使其成为浮点数。
让我们看看 C++ 标准
§ 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.
(抱歉发布了所有内容,但您可以通过这种方式了解有关 float
的更多信息)
我正在重载函数 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: " << i;
}
void add(float c){
f=c;
cout << "Second Int: " << f;
}
};
int main(){
students obj;
obj.add(9);
obj.add(5.5);
}
错误:
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
是 double
,但 none 的函数采用 double
参数。因此,编译器会混淆是调用带有 int
参数的函数,还是调用带有 float
参数的函数。所以,你会收到一条错误消息,说它不明确。
这就是为什么当您将函数更改为具有 double
参数时,错误不再出现,因为现在有一个函数可以接受 double
参数,因此有那里有歧义。
您也可以通过调用函数 as
来解决问题obj.add(5.5f);
在数字后添加 f
使其成为浮点数。
让我们看看 C++ 标准
§ 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.
(抱歉发布了所有内容,但您可以通过这种方式了解有关 float
的更多信息)