Click here to Skip to main content
15,868,306 members
Please Sign up or sign in to vote.
4.50/5 (7 votes)

When I'm trying...

C++
void function(int y,int w)
{
    printf("int function");

}


void function(float y,float w)
{
   printf("float function");
}

 
int main()
{
   function(1.2,2.2);
   return 0;
}

I get an error error like..

error C2668: 'function' : ambiguous call to overloaded function

and when I'm trying to call function(1.2,2) or function(1,2.2) it is printing as "int function"

Please clarify..when will the function(float y,float w)?

Posted
Updated 4-Sep-09 8:50am
v5

In addition to what Stuart said, if you really don't want to change the function signature from floatto double, you can always use literals that are typed as float. If you add the suffix f to the floating point numbers, they will be typed float.

Your examples would then be function(1.2f, 2f) and function(1, 2.2f).

 
Share this answer
 
Look at the error message from gcc:

a.cpp:16: error: call of overloaded ‘function(double, double)’ is ambiguous
a.cpp:3: note: candidates are: void function(int, int)
a.cpp:9: note:                 void function(float, float)


A call to either function would require truncation, which is why neither is preferred over the other. I suspect you really want void function(double y,double w). Remember that in C/C++, the default floating-point type for literals and parameter passing is double, NOT float.
 
Share this answer
 
v2
Just imagine how your arguments would be passed.

If it is passed as 1.2 and 2.2 to the (int,int) function then it would to truncated to 1 and 2.

If it is passed as 1.2 and 2.2 to the (float,float) it will be processed as is.

So here is where the ambiguity creeps in.

I have found two ways to solve this problem.
First is the use of literals:-
int main()
{
        function(1.2F,2.2F);
	return 0;
}


Secondly, and the way I like to do it, It always works (and can also be used for C++'s default conversion and promotion).
For int:-
int main()
{
int a=1.2, b=2.2;
	function(a,b);
	return 0;
}


For Float:-
int main()
{
float a=1.2, b=2.2;
	function(a,b);
	return 0;
}


So instead of using actual DIGITS. It is better to declare them as a type first, then overload!

See now, if you send it as (1.2,2) or (1,2.2) then compiler can simply send it to the int function and it would work.
However, to send it to the float function the compiler would have to promote 2 to float. Promotion only happens when no match is found.

Refer:-
Computer Science with C++
Sumita Arora
Chapter: Function Overloading
 
Share this answer
 
your function overloading part is clear.
if you write 1.2 by default it is treated as double.
a double( of size 8) is can be converted into float(of size 4) and int (of size 4 ) , so the compiler confusion ( ambiguity).

solutions.
C#
function((float)1.2,(float)2.2);
function(1.2F,2.2F);
 
Share this answer
 
Java
class a
{
public void dis()
{
System.out.println("no argument");
}
public void dis(int i)
{
System.out.println("one argument");
}
public void dis(int i,int j)
{
System.out.println("two argument");
}

class maindemo
{
public static void main(String aggs[])
{
a=obj=new a();
a.dis();
a.dis(10,20);
a.dis(10);
}
}



this code use java programing
 
Share this answer
 
v2
Comments
Aescleal 4-Jun-10 2:55am    
Reason for my vote of 1
That's a great help to a C programmer...
It is better to write like this:
template<class T>
T function_name(T a, T b)
{
// add your code here.
}
 
Share this answer
 
Use double instead of Float.............

Bing !!!!!!!!!!!
 
Share this answer
 
Comments
Smithers-Jones 3-Jun-10 15:22pm    
"Bing !!!!!!!!!!!" says the one who can't google.
Sandeep Mewara 3-Jun-10 15:23pm    
Reason for my vote of 1
Why answering a question that was already marked answered that too more 6 months old?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900