0

I am getting compiler error of overloaded function is ambiguous. although i understand what it means but how to resolve this problem?

         #include <iostream>
         using namespace std;
         int area(int );
         int area(int ,int );
         float area(float );
         
         int main()
         {
                cout << "square: " << area(5) << "\n";
                cout << "rectangle: " << area(22,14) << "\n";
                cout << "circle: " << area(6.5) << "\n";

                return 0;
         }

         inline int area(int a)
         {
               return (a*a);
         }

         inline int area(int b,int c)
         {
                return (b*c);
         }

         float area(float d)
         {
              return (3.14*d*d);
         }
  • 1
    `6.5` is a `double`. You can use `6.5f`. – kotatsuyaki Dec 05 '22 at 05:28
  • 1
    See [dupe](https://stackoverflow.com/questions/62159885/why-is-an-overloaded-function-with-two-arguments-of-type-double-called-when-pass) which explains what is happening. – Jason Dec 05 '22 at 05:31

1 Answers1

2

This is because 6.5 is a double not a float, the compiler then tries to find area(double) which doesn't exist. It's ambiguous between area(float) and area(int).

One solution is that you should call it using 6.5 as a float.

area(6.5f)

Cedric Martens
  • 1,139
  • 10
  • 23