c语言pow对重载函数的调用不明确,关于c ++:使用float会导致“对重载函数的调用不明确”错误...

本问题已经有最佳答案,请猛点这里访问。

我正在重载函数add(),但是当我使用float数据类型时,它显示了一个错误。 但是,当我将其更改为double时,它可以正常工作。 为什么float导致错误?

代码是:

#include

using namespace std;

class students{

private:

int i;

float f;

public:

void add(int b){

i=b;

cout <

}

void add(float c){

f=c;

cout <

}

};

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)

将obj.add(5.5);更改为obj.add(5.5f);。

5.5是双精度数而不是浮点数。 5.5f是浮点文字。

Oooh Waoo太好了,没什么改变。.您能解释一下f浮动的区别吗?

由于您对正在发生的事情感到困惑,因此显而易见的是,只有两个方法仅根据参数类型进行区分,而做不同的事情,这是一个非常非常糟糕的主意。 现在考虑在不同的实现中,例如size_t可以是unsigned int,long或long long,因此,如果执行这种重载,则f(sizeof(1))可以调用不同的方法!

5.5是double,但是您的函数均不使用double自变量。 因此,编译器对于是使用int参数调用函数还是使用float参数调用函数感到困惑。 因此,您会收到一个错误消息,说它是模棱两可的。

这就是为什么当您将函数更改为具有double参数时,不再出现错误的原因,因为现在有一个函数可以接受double参数,因此那里存在歧义。

您也可以通过调用以下函数来解决问题:

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的更多信息)

嗨,谢谢分配问题解决。.但是我对f字面有点困惑,您能解释一下,只是float需要f吗? 为什么不要求加倍D? 它是c ++语言规则吗? 您能给我任何关键字进行搜索吗? 或任何链接? 对不起,我是C ++新手

@AsifMushtaq,您不能将其转换为带有d的double,对于float来说是f可用,但是对于double来说没有d。

zh.cppreference.com/w/cpp/language/floating_literal

@ ArunA.S好吧.. !! 也感谢Alank Stokes

@AsifMushtaq,我已经从c ++标准中摘录了一小段(好的,虽然很大,但是与标准给出的所有内容相比,它的确很小)。

好@ ArunA.S懂了!!! 再次感谢

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值