Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C++/CLI C MFC ATL WTL STL

When I'm trying...

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 22-Aug-09 4:28am
Edited 4-Sep-09 9:50am
0x3c033.2K
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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).

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

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.
function((float)1.2,(float)2.2);
function(1.2F,2.2F);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  
v2
Comments
Aescleal at 4-Jun-10 2:55am
   
Reason for my vote of 1
That's a great help to a C programmer...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

It is better to write like this:
template<class T>
T function_name(T a, T b)
{
   // add your code here.
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Use double instead of Float.............
 
Bing !!!!!!!!!!!
  Permalink  
Comments
Smithers-Jones at 3-Jun-10 15:22pm
   
"Bing !!!!!!!!!!!" says the one who can't google.
Sandeep Mewara at 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)

  Print Answers RSS
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 5 Jul 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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