1

I have a function fun() I wish to overload in the same scope. As per the rules of overloading, different order of arguments should allow for the overloading of the function as mentioned here.

The Code:

#include "iostream"
using namespace std;
void fun(int i, float j)
{
        cout << "int,float";
}

void fun(float i, int j)
{
        cout << "float,int";
}

int main()
{
        fun(20,20);
}

Error:

error: call of overloaded ‘fun(int, int)’ is ambiguous
   15 |         fun(20,20);

Question:

Had there been only one function with argument fun(int, float), that would have been called as the best match, so why does it throw error in this case.

inarticulatus
  • 175
  • 1
  • 10
  • 2
    Which one do you want? `fun(20, 20.f)` or `fun(20.f, 20)`? – Jarod42 Jul 21 '21 at 09:08
  • 9
    Which `20` should be converted to `float`? First or second? Compiler can't decide on its own, both would be as good, so it's ambiguous – Yksisarvinen Jul 21 '21 at 09:08
  • 1
    "Different order of arguments should allow for the overloading of the function" -- yes, insofar as **identical** orders of arguments (rather, argument *types*) does not allow for overloading *at all*. Your ambiguity comes, not from the *definition* of the overload, but from the *calling* of the overload. The example in your tutorial assumes that you are *calling* those functions with either `double, int` or `int, double`. – DevSolar Jul 21 '21 at 09:15
  • @DevSolar So bear with me on this. **Now** I understand the type conversion would virtually be same. However, there would be some order of preference right? My understanding from reading [Microsoft Documents](https://learn.microsoft.com/en-us/cpp/cpp/function-overloading?view=msvc-160#argument-matching) was that it would first match the first argument. Or does that example not account for such case? – inarticulatus Jul 21 '21 at 09:20
  • 1
    @inarticulatus: no, that is not how function overloads are resolved. The two candidates are equally good, so the call is ambiguous. (Your Microsoft link says nothing about treating the first argument differently.) – TonyK Jul 21 '21 at 09:29
  • @TonyK I was referring to the example with Variant 1, 2, and 3, where they choose Variant 1 and 3 based on the fact that first argument in both Variant 1 and 3 are Fractions – inarticulatus Jul 21 '21 at 09:30
  • 1
    OT: it should be `#include ` instead of `#include "iostream"` – Jabberwocky Jul 21 '21 at 09:31
  • I was using g++ compiler and the example I followed had written it that way, is it a convention to write <> and not " " – inarticulatus Jul 21 '21 at 09:33
  • 1
    Set 1 considers the first argument, and Set 2 considers the second. So the two arguments are treated identically. – TonyK Jul 21 '21 at 09:36
  • 1
    @inarticulatus: Generally `<>` is for the standard library (and may not be actually a file), `""` for everything else. See [this answer](https://stackoverflow.com/a/50266/60281) for details. – DevSolar Jul 21 '21 at 09:36

2 Answers2

2

You give two integers, so the compiler have to convert one into a float, but which function shall be taken?

int main()
{
  fun(20.0,20);
  fun( 20, 20.0);
}

these calls makes the compiler happy, since you tell which function shall be taken.

MiniMik
  • 268
  • 1
  • 10
  • 5
    Those floating point literals are not single precision (float) but double. 20.0 is double and 20.0f is single precision literal. – Ivan Vnucec Jul 21 '21 at 09:14
2

The reason why you are facing this error is due to the data type of the arguments you have provided In this case

fun(20,20);

which are both int, int.

A correct function call in this scenario would be,

fun(20,20.0);

or

fun(20.0,20);

(edit) as mentioned by Ivan Vnucec it would be better to explicitly call the function with arguments that are float instead of double so call it in this way:

fun(20,20.0f);

or

fun(20.0f,20);
Deyuan
  • 56
  • 1
  • 6