overloaded function in C++

35 Views Asked by At

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

There are 1 best solutions below

1
On

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)