This question already has answers here:
Strange ambiguous call to overloaded function error
(11 answers)
Closed 6 years ago.
Why is this cause a compiler error, stating that my references are ambiguous? I have a float, int and string, which should all create separate function signatures, right?
Here is what I have so far:
#include <iostream>
#include <string>
using namespace std;
int plus(int a, int b);
float plus(float a, float b);
string plus(string a, string b);
int main(void)
{
int n = plus(3, 4);
double d = plus(3.2, 4.2);
string s = plus("he", "llo");
string s1 = "aaa";
string s2 = "bbb";
string s3 = plus(s1, s2);
}
int plus(int a, int b) {
return a+b;
} // int version
float plus(float a, float b) {
return a+b;
} // float version
string plus(string a, string b) {
return a+b;
} // string version
First, don't use using namespace std;. In this case, there happens to be a struct called std::plus—oh, wait, never mind, that's actually called plus and its constructor is thrown into the bucket for overload resolution with your function called plus.
Second, you have an ambiguity because 3.2 and 4.2 are of type double and can convert equally well to float or int.
This is a simplification, but when it comes to passing numbers to an overloaded function, C++ basically uses these rules:
If all the arguments exactly match one of the overloads, then use that one.
If all the arguments can be promoted to match one of the overloads, then use that one.
If all the arguments can be numerically converted to match one of the overloads, then use that one.
If you have multiple candidates at a specific level, then that is an ambiguity. Crucially, a double does not promote to a float—that would be a downgrade. So it has to use a standard conversion to a float, which ties with the standard conversion to an int, so those two overloads are ambiguous.
Call the float overload like this:
double d = plus(3.2f, 4.2f);
A constant like 3.2 actually has type double.
Related
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 try 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) be called?
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.
UPDATE
If you really don't want to change the function signature from float to 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).
What is operator overloading?
Sbi's famous Operator overloading faq answers this in great detail.
Why are the two function versions in OP allowed to exist?
Notice they take different function parameter types(int and float) and hence qualify as valid function overloads.
What is overload resolution?
It is the process of selecting the most appropriate function/operator by the compiler implementation. If a best viable function exists and is unique, overload resolution succeeds and produces it as the result. Otherwise overload resolution fails and the invocation is treated as ill-formed and compiler provides a diagnostic. The compiler uses implicit conversion sequence to find the best match function.
C++03 Standard 13.3.3.1 Implicit Conversions:
An implicit conversion sequence is a sequence of conversions used to convert an argument in a function call to the type of the corresponding parameter of the function being called.
The implicit conversion sequences can be one of the following categories:
A standard conversion sequence(13.3.3.1.1)
A user-defined conversion sequence(13.3.3.1.2)
An ellipsis conversion sequence(13.3.3.1.3)
Note that each of these are ranked to determine the best viable function. The best viable function is the one all whose parameters have either better or equal-ranked implicit conversion sequences than all of the other viable functions.The standard details each of these in detail in respective sections. The standard conversion sequence is relevant to this case, it is summarized as:
With enough background on overloading resolution.
let us examine the code examples in OP:
function(1.2,2.2);
Important Rule: 1.2 and 2.2 are literals and they are treated as a double data type.
During implicit conversion sequences mapping:
Both the function parameter literals with double type need a conversion rank to either call the float or int version and none is a better match than other, they score exactly the same on conversion rank. The compiler is unable to detect the best viable match and it reports an ambiguity.
function(1.2,2);
During implicit conversion sequence mapping:
One of the function parameters 2 has an exact match with the int function version while another 1.2 has a conversion rank. For function which takes float as parameters the implicit conversion sequences for both parameters are of conversion rank.
So the function which takes int version scores better than the float version and is the best match and gets called.
How to resolve overloading ambiguity errors?
If you don't want the implicit conversion sequence mapping to throw you off, just provide functions and call them in such a way so that the parameters are a exact match. Since exact match scores over all others, You have a definite guarantee of your desired function getting called. In your case there are two ways to do this:
Solution 1:
Call the function so that parameters are exact match to the functions available.
function(1.2f,2.2f);
Since 1.2f and 2.2f are treated as float types they match exactly to the float function version.
Solution 2:
Provide a function overload which exactly matches the parameter type in called function.
function(double, double){}
Since 1.2 and 2.2 are treated as double the called function is exact match to this overload.
If you don't want to (as explained in the accepted answer):
use float literals, e.g. 1.2f
or change the existing float overload to double
You can add another overload that calls the float one:
void function(double y, double w)
{
function((float)y, (float)w);
}
Your code in main now will call the above function, which will call the float overload.
Function overloading in the above example has ambiguous calls because the return type are same and the 2nd argument in the call of function is double, which can be treated as int or float and hence the compiler confuses to which function to execute.
I hope this help
This code is self explaintary for all combination
You need to send two float to call a float function
#include<iostream>
#include<stdio.h>
using namespace std;
//when arguments are both int
void function(int y,int w) {
printf("int function\n");
}
//when arguments are both double
void function(double y, double w) {
printf("double function\n");
}
//when arguments are both float
void function(float y, float w) {
printf("float function\n");
}
//when arguments are int and float
void function(int y, float x) {
printf("int float function\n");
}
//when arguments are float and int
void function(float y,int w) {
printf("float int function\n");
}
//when arguments are int and double
void function(int y, double w) {
printf("int double function\n");
}
//when arguments are double and int
void function(double y, int x) {
printf("double int function\n");
}
//when arguments are double and float
void function(double y, float x) {
printf("double float function\n");
}
//when arguments are float and double
void function(float y, double x) {
printf("float double function\n");
}
int main(int argc, char *argv[]) {
function(1.2,2.2);
function(1.2f,2.2f);
function(1,2);
function(1.2,2.2f);
function(1.2f,2.2);
function(1,2.2);
function(1,2.2f);
function(1.2,2);
function(1.2f,2);
return 0;
}
When sending a primitive type to a function as argument, if the primitive type you are sending is not exactly the same as it requests, you should always cast it to the requested primitive type.
int main()
{
function(1.3f, 2.4f);
function(1.3f, static_cast<float>(2.4));
function(static_cast<float>(1.3), static_cast<float>(2.4));
function(static_cast<float>(1), static_cast<float>(2));
return 0;
}
By default decimal is considered as double. If you want decimal to be floats you suffix it with f.
In your example when you call function(1.2,2.2) the compiler considers the values you have passed it as double and hence you are getting mismatch in function signature.
function(1.2,1.2) ====> function(double,double)
If you want to retain the function signature you need to use floating point suffix while passing floating point literal.
function(1.2f,1.2f) ====> function(float,float).
If you are more interested in knowing about floating point literals you can refer
Why floating point value such as 3.14 are considered as double by default in MSVC?
Like others have said, you give doubles to your overloaded function which is designed for floats. The overloading itself doesn't have any errors.
Here's the correct use of the overloaded function (notice the 'f'´s right after the numbers):
function(1.0f, 2.0f);
function(1.2,2.2);
Those numbers aren't floats, they are doubles. So this code says:
double p1 = 1.2;
double p2 = 2.2;
void (*fn)(double /*decltype(p1)*/, double /*decltype(p2)*/) = function;
The compiler is now looking a "function" which takes two doubles. There is no exact match. So next it looks for a function which takes an argument that can be cast from doubles. There are two matches.
function(int, int);
function(float, float);
You have several options.
Add an exact match overload.
void function(double, double)
{
printf("double function\n");
}
Use casting.
function(static_cast(1.2), static_cast(2.2));
Call "function" with floats instead of doubles:
function(1.2f, 2.2f);
Try This
#include <iostream>
using namespace std;
void print(int i){
cout << i << endl;
}
void print(float i){
cout << i << endl;
}
int main(){
print(5);
print(5.5f);
return 0;
}
In function overloading when float can conflict with other data type in other same name functions then probably this is way to over come it. I tried it worked.
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
When using function with same name, parameter list must differ(either in type of parameter or number of parameters used). I was just practicing with this concept. I wrote the following code.
#include <iostream>
int myFunction(int n)
{
return 2*n;
}
float myFunction(float n)
{
return 3*n;
}
int main()
{
int x=myFunction(3);
std::cout << x;
return 0;
}
I thought I will get error because compiler will get confused which myFunction to use because I directly pass the value 3 without storing it in a particular type of variable . But I got output 6. So I tried the following code.
#include <iostream>
int myFunction(int n)
{
return 2*n;
}
float myFunction(float n)
{
return 3*n;
}
int main()
{
float x=myFunction(3.3);
std::cout << x;
return 0;
}
As previous one worked fine, I thought this will work fine too, as 3.3 is not integer so it's clear which one to call, but this time I got compiler error saying it's ambiguous.
So my doubt is why first code worked but not second one.
The process of selecting the overload during a call is called overload resolution. Given the types of the arguments, the compiler selects the best viable function from the list of candidates - the one that can be invoked with the least amount of promotions and implicit conversions.
In the first case the first one myFunction(int) requires 0 conversions for an int argument (3), and the second one requires one conversion (int -> float), so the first one is selected as the best candidate.
In the second case a double argument (3.3) requires a conversion to either int or float, so there is no clear winner and thus the call is ambiguous.
The fix could be to use a float argument (3.3f) or change myFunction(float) to myFunction(double).
Literals have types too. As integer literal 3 is of type int, then the 1st overload is selected.
As floating point literal 3.3 is of type double (but not float; with the suffix f like 3.3f the type is determined as float), the calling is ambiguous because it could convert to both int and float implicitly.
try this:
int x=myFunction(int(3));
float x=myFunction(float(3.3));
This question already has answers here:
Strange ambiguous call to overloaded function error
(11 answers)
Closed 1 year ago.
I've been programming a pair of overloaded C++ functions, one taking 2 integer parameters, and the other 2 floats.
But the codeblocks compiler says:
error: call of overloaded 'func(double, double)' is ambiguous
Why double if I'm specifying a float?
I'm using the two functions to sum their values and showing the result on cout inside them. Float values given as arguments are 1.14 and 3.33, not big floating numbers...
Someone knows? thx!
#include <iostream>
using namespace std;
void func(int a, int b) {
cout << a+b;
}
void func(float a, float b)
cout << a+b;
}
int main() {
func(3, 7);
func(1.14, 3.33);
}
The function call func(1.14, 3.33) is ambiguous because 1.14 and 3.33 are doubles and both can be converted to either int or float. Therefore the compiler does not know which function to call.
You can fix this by explicitly specifying the type of the constants func(float(1.14), float(3.33)) or by changing the overload from func(float, float) to func(double, double).
In this case, explicitly telling the compiler which type to use is probably a better idea.
I am trying to create a simple (absolute) function in c++, I have created two functions with the same name one that takes an integer and returns an integer and one that takes a float and returns a float but every time I try to run the code I receive this error:
"error: call of overloaded 'absolute(double)' is ambiguous"
I tried changing the input parameters of the second function so that it takes a double and returns a float and the code ran perfectly, I'd like to know why the code won't run when the parameters and return type are both set to float, thank you.
#include <iostream>
#include <fstream>
using namespace std;
int absolute(int x){
if (x<0){
x=-x;
}
return x;
}
float absolute (float x)
{
if (x<0){
x=-x;
}
return x;
}
int main( )
{
cout << absolute(3.5);
}
The type of the literal 3.5 is double, not float.
Choosing either of the overloads would require a conversion. Hence the ambiguity.
You can use 3.5f to make it a float literal.
cout << absolute(3.5f);
A better solution, IMO, would be to use a function template.
template <typename T>
T absolute(T x)
{
return (x < 0 ? -x : x);
}
Read that error message again. Notice how it says double as the argument type it want to use.
That's because floating point constants like 3.5 are of type double. And the compiler don't know if it should convert the double value to an int or a float, thereby giving you the error.
If you want to call the float overload, use 3.5f to make it a float value. Or change your overload to use type double instead of float.
you write that 3.5 is a float this value is not a float it is a double.
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 try 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) be called?
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.
UPDATE
If you really don't want to change the function signature from float to 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).
What is operator overloading?
Sbi's famous Operator overloading faq answers this in great detail.
Why are the two function versions in OP allowed to exist?
Notice they take different function parameter types(int and float) and hence qualify as valid function overloads.
What is overload resolution?
It is the process of selecting the most appropriate function/operator by the compiler implementation. If a best viable function exists and is unique, overload resolution succeeds and produces it as the result. Otherwise overload resolution fails and the invocation is treated as ill-formed and compiler provides a diagnostic. The compiler uses implicit conversion sequence to find the best match function.
C++03 Standard 13.3.3.1 Implicit Conversions:
An implicit conversion sequence is a sequence of conversions used to convert an argument in a function call to the type of the corresponding parameter of the function being called.
The implicit conversion sequences can be one of the following categories:
A standard conversion sequence(13.3.3.1.1)
A user-defined conversion sequence(13.3.3.1.2)
An ellipsis conversion sequence(13.3.3.1.3)
Note that each of these are ranked to determine the best viable function. The best viable function is the one all whose parameters have either better or equal-ranked implicit conversion sequences than all of the other viable functions.The standard details each of these in detail in respective sections. The standard conversion sequence is relevant to this case, it is summarized as:
With enough background on overloading resolution.
let us examine the code examples in OP:
function(1.2,2.2);
Important Rule: 1.2 and 2.2 are literals and they are treated as a double data type.
During implicit conversion sequences mapping:
Both the function parameter literals with double type need a conversion rank to either call the float or int version and none is a better match than other, they score exactly the same on conversion rank. The compiler is unable to detect the best viable match and it reports an ambiguity.
function(1.2,2);
During implicit conversion sequence mapping:
One of the function parameters 2 has an exact match with the int function version while another 1.2 has a conversion rank. For function which takes float as parameters the implicit conversion sequences for both parameters are of conversion rank.
So the function which takes int version scores better than the float version and is the best match and gets called.
How to resolve overloading ambiguity errors?
If you don't want the implicit conversion sequence mapping to throw you off, just provide functions and call them in such a way so that the parameters are a exact match. Since exact match scores over all others, You have a definite guarantee of your desired function getting called. In your case there are two ways to do this:
Solution 1:
Call the function so that parameters are exact match to the functions available.
function(1.2f,2.2f);
Since 1.2f and 2.2f are treated as float types they match exactly to the float function version.
Solution 2:
Provide a function overload which exactly matches the parameter type in called function.
function(double, double){}
Since 1.2 and 2.2 are treated as double the called function is exact match to this overload.
If you don't want to (as explained in the accepted answer):
use float literals, e.g. 1.2f
or change the existing float overload to double
You can add another overload that calls the float one:
void function(double y, double w)
{
function((float)y, (float)w);
}
Your code in main now will call the above function, which will call the float overload.
Function overloading in the above example has ambiguous calls because the return type are same and the 2nd argument in the call of function is double, which can be treated as int or float and hence the compiler confuses to which function to execute.
I hope this help
This code is self explaintary for all combination
You need to send two float to call a float function
#include<iostream>
#include<stdio.h>
using namespace std;
//when arguments are both int
void function(int y,int w) {
printf("int function\n");
}
//when arguments are both double
void function(double y, double w) {
printf("double function\n");
}
//when arguments are both float
void function(float y, float w) {
printf("float function\n");
}
//when arguments are int and float
void function(int y, float x) {
printf("int float function\n");
}
//when arguments are float and int
void function(float y,int w) {
printf("float int function\n");
}
//when arguments are int and double
void function(int y, double w) {
printf("int double function\n");
}
//when arguments are double and int
void function(double y, int x) {
printf("double int function\n");
}
//when arguments are double and float
void function(double y, float x) {
printf("double float function\n");
}
//when arguments are float and double
void function(float y, double x) {
printf("float double function\n");
}
int main(int argc, char *argv[]) {
function(1.2,2.2);
function(1.2f,2.2f);
function(1,2);
function(1.2,2.2f);
function(1.2f,2.2);
function(1,2.2);
function(1,2.2f);
function(1.2,2);
function(1.2f,2);
return 0;
}
When sending a primitive type to a function as argument, if the primitive type you are sending is not exactly the same as it requests, you should always cast it to the requested primitive type.
int main()
{
function(1.3f, 2.4f);
function(1.3f, static_cast<float>(2.4));
function(static_cast<float>(1.3), static_cast<float>(2.4));
function(static_cast<float>(1), static_cast<float>(2));
return 0;
}
By default decimal is considered as double. If you want decimal to be floats you suffix it with f.
In your example when you call function(1.2,2.2) the compiler considers the values you have passed it as double and hence you are getting mismatch in function signature.
function(1.2,1.2) ====> function(double,double)
If you want to retain the function signature you need to use floating point suffix while passing floating point literal.
function(1.2f,1.2f) ====> function(float,float).
If you are more interested in knowing about floating point literals you can refer
Why floating point value such as 3.14 are considered as double by default in MSVC?
Like others have said, you give doubles to your overloaded function which is designed for floats. The overloading itself doesn't have any errors.
Here's the correct use of the overloaded function (notice the 'f'´s right after the numbers):
function(1.0f, 2.0f);
function(1.2,2.2);
Those numbers aren't floats, they are doubles. So this code says:
double p1 = 1.2;
double p2 = 2.2;
void (*fn)(double /*decltype(p1)*/, double /*decltype(p2)*/) = function;
The compiler is now looking a "function" which takes two doubles. There is no exact match. So next it looks for a function which takes an argument that can be cast from doubles. There are two matches.
function(int, int);
function(float, float);
You have several options.
Add an exact match overload.
void function(double, double)
{
printf("double function\n");
}
Use casting.
function(static_cast(1.2), static_cast(2.2));
Call "function" with floats instead of doubles:
function(1.2f, 2.2f);
Try This
#include <iostream>
using namespace std;
void print(int i){
cout << i << endl;
}
void print(float i){
cout << i << endl;
}
int main(){
print(5);
print(5.5f);
return 0;
}
In function overloading when float can conflict with other data type in other same name functions then probably this is way to over come it. I tried it worked.
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