以下程序有何含糊之处? [英] What is ambiguity in following program?

查看:88
本文介绍了以下程序有何含糊之处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream>
using namespace std;
int add(int,int);
float add(float,float);
void main()
{
	cout<<add(10.20,60.1);
 

}
int add(int a,int b)
{
	return a+b;
}
float add(float c,float d)
{
	return c+d;
}





我正在传递浮动值,但为什么显示模棱两可?



I am passing floating value but it show ambiguity why?

推荐答案

根据编译器的不同,实际上可能传递的是double值而不是float值。尝试在通话结束时使用f。



Depending on the compiler, you may actually be passing a double value instead of a float. Try using the "f" on the end of your call.

add(10.2f, 60.1f)





我想(试图从内存开始)可以将double转换为float和int,但必须明确地完成。在没有告诉它使用哪一个的情况下,编译器无法为您选择,因为它们都在缩小转换(丢失数据)并且它告诉您自己选择一个。



I think (trying to go from memory here) that a double can be cast to both a float and an int but must be done explicitly. Without telling it which one to use, the compiler can't choose for you since they are both narrowing conversions (lose data) and it tells you to pick one yourself.


Ron有点要点什么是错的 - 你没有传递 float s:10.20和60.1是 double 常数。



要么使常量浮动 s(10.20f和60.1f)或添加 double 函数的重载。
Ron's got the gist of what's wrong - you're not passing floats: 10.20 and 60.1 are double constants.

Either make your constants floats (10.20f and 60.1f) or add a double overload for the function.


这篇关于以下程序有何含糊之处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆