0
//function overloading
#include <iostream>
using namespace std;

//declaration of function protype
int area(int);
int area(int, int);
float area(float);

int main()
{
    cout << "calling the area() function for computiong the area of a square (side=5) - " << area(5) << "\n";
    cout << "calling the area() function for computiong the area of a rectangle (length = 5, bredth = 10) - " << area(5, 10) << "\n";
    cout << "calling the area() function for computiong the area of a cirlce (radius 5.5) - " << area(5.5) << "\n";
    return 0;
}

int area(int side)
{
    return (side * side);
}
int area(int length, int breadth)
{
    return (length * breadth);
}
float area(float radius)
{
    return (3.14 * radius * radius);
}

error

program4_5.cpp: In function 'int main()':
program4_5.cpp:14:106: error: call of overloaded 'area(double)' is ambiguous
   14 |     cout << "calling the area() function for computiong the area of a cirlce (radius 5.5) - " << area(5.5) << "\n";
      |                                                                                                          ^
program4_5.cpp:6:5: note: candidate: 'int area(int)'
    6 | int area(int);
      |     ^~~~
program4_5.cpp:8:7: note: candidate: 'float area(float)'
    8 | float area(float);
      |       ^~~~
PS C:\Users\hasht\Desktop\coding\OOP with c++> 

this code while compiling shows function overloading error but from what I know it's correct as I have defined the float area( float radius) function. Can anyone explain why this error occurs. I am new to coding and i don't know how to fix this.

273K
  • 29,503
  • 10
  • 41
  • 64
Irfan Asif
  • 35
  • 2
  • 7
  • C does not have namespaces, iostream header and function overloading, so please don't spam the tag C. – 273K Mar 31 '21 at 06:43
  • Voting for closing. This question has so many answers: [Using float gives “call to overloaded function is ambiguous” error](https://stackoverflow.com/questions/30003519/using-float-gives-call-to-overloaded-function-is-ambiguous-error) which is a duplicate of [Reference to function is ambiguous](https://stackoverflow.com/questions/15037705/reference-to-function-is-ambiguous) which is a duplicate of [Strange ambiguous call to overloaded function error](https://stackoverflow.com/questions/16602175/strange-ambiguous-call-to-overloaded-function-error) – user1810087 Mar 31 '21 at 07:43

2 Answers2

0

5.5 is a literal of type double, so the compiler doesn't know you want to call the overload taking an int or the one taking a float.

You can use a float-literal instead:

//                      -V-
cout << "..." << area(5.5f) << "\n";
Lukas-T
  • 11,133
  • 3
  • 20
  • 30
-1

Type area(5.5f) to force the value to be a float.

The compiler does not know if it should cast your double value to an int or a float. Therefore, it is ambiguous.

CYBERCAV
  • 151
  • 6