Click here to Skip to main content
14,210,773 members
Rate this:
Please Sign up or sign in to vote.
See more:
using namespace std;
int add(int,int);
float add(float,float);
void main()

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?
Sergey Alexandrovich Kryukov 12-Dec-13 14:16pm
In practice, you need to review your interface. These two functions are not practically sensible. If you design practically sensible interface, and do it accurately, then number of similar problems will be naturally reduced to bare minimum, if they even ever appear. Please see my comment to Solution 1. If this is unclear, I'll explain it.
However, I do appreciate your desire to get to the root of the problem.
Rate this:
Please Sign up or sign in to vote.

Solution 1

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)

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.
Sergey Alexandrovich Kryukov 12-Dec-13 14:09pm
5, but I was rather pointed out that OP's sample is the example of the poor choice of function naming. So called "operator overloading" is only good where the compiler's ability to resolve ambiguity is well used. The example of good use: different number of parameters, the use of the completely mutually unassignable argument types at the same position.
Vishal I. Sahuji 12-Dec-13 16:08pm
But actually i did not write function for double then why compiler confuse?
yes if i write one more function for double then compiler should confuse between float and double but i did not write then how compiler is get confuse?
Ron Beyer 12-Dec-13 16:12pm
Because both are narrowing conversions, so it will not pick them for you. Yes, it seems like Float is closest to double, but in fact they are both the same size byte wise and the compiler will not make a guess as to which one you want. You need to specify, that's just how it works.
BillW33 13-Dec-13 11:18am
You have one add method that takes an int and one add method that takes a float, but you are calling the add method [add(10.20,60.1)] with doubles. When you write a decimal number such as 10.20 the compiler treats it as a double unless you put an f after it to tell the compiler that the number is a float. That is why Ron Beyer recommended using "add(10.2f, 60.1f)".
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.
Aescleal 12-Dec-13 17:37pm
Hi Mr. Univoter, any clue as to what's factually incorrect with my answer? I'd like to know so I can improve my practice in the future. I don't indulge in tit-for-tat down voting so please don't worry about some sort of petty retaliation.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190618.1 | Last Updated 12 Dec 2013
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100