Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to program an overload function but I get an ambiguous call error:

here is the code:
C++
#include <iostream>
#include <string.h>

using namespace std;
int FindArea(int radius, int height, int Pi = 3.1415)
{
    return Pi * radius * radius * height;
}

int FindArea(int radius, int Pi = 3.1415)
{
    return 4 * Pi * radius * radius * radius / 3;
}

int main()
{
    int rad, heigh;
    cout << "enter c for cylinder, s for sphere: ";
    string choice;
    cin >> choice;
    
    if (choice == "c")
    {
        cout << "enter the radius and height: ";
        cin >> rad >> heigh;
        FindArea(rad, heigh)
    }
    else if (choice == "s")
    {
        cout << "enter the radius: ";
    }
}
i havent fully completed it, but i get the ambiguous call error when i call the function FindArea inside of a if statement. Pls help me and show the solution as well if you can.

What I have tried:

i have tried finding solution but i cant seen to find any regarding to my problem
Posted
Updated 24-Jun-21 1:58am
v2
Comments
Richard MacCutchan 24-Jun-21 7:04am    
Since PI is always constant there is no need to pass it as a parameter. And why are you using int types for what should be (and in Pi's case is) floating point values.

The problem is that both versions of FindArea can be called with two arguments. When a parameter provides a value, as in
C++
int Pi = 3.1415
it means it is an optional default, so the compiler can't decide which of the two functions is intended.

On an unrelated note, these are finding volumes, not areas, so the names would confuse a user. And unless you want to allow the user to specify the value of Pi, just make it a constant:
C++
constexpr double Pi = 3.1415;
Remove it as a parameter and just use it within your functions. It has to be defined before them so that the compiler will know what it is when it sees it.
 
Share this answer
 
v5
To add to what Greg has said: when you call FindArea, you are passing it two integer values:
C++
FindArea(rad, heigh)
The first definition accespts three parameters with the last one being optional, it matches:
C++
int FindArea(int radius, int height, int Pi = 3.1415)
But ... the second definition can also take two integer parameters:
C++
int FindArea(int radius, int Pi = 3.1415)

So the system isn't sure which one you meant to call, and won't compile your code unless it decided wrongly!

There are also other problems with your code:
C++
int FindArea(int radius, int Pi = 3.1415)
Will give you a defaulted value for Pi of three - which will create some very odd circles ...
If you want to pass a floating point value, you need to define your function as accepting a floating point value - and you probably want to return a floating point value as well!
C++
double FindArea(int radius, int height, double Pi = 3.1415)
    {
    return Pi * radius * radius * height;
    }
    
double FindArea(int radius, double Pi = 3.1415)
    {
    return 4 * Pi * radius * radius * radius / 3;
    }

In fact, that cures your problem as well - because now when you try to call your function like this:
C++
FindArea(rad, heigh)
Only the first definition matches, so the call is no longer ambiguous.

But ... you have to ask yourself why are you passing Pi as a value at all? It's is a universal constant and really can't be changed! :laugh:
 
Share this answer
 
Try also
C++
#include <iostream>
#include <memory>
#include <cmath>
using namespace std;

class Shape
{
public:
  virtual double getArea() = 0;
};

class Cylinder: public Shape
{
  double radius, height;
public:
  Cylinder(double radius, double height):radius{radius}, height{height}{}
  double getArea() override { return 2 * M_PI * radius * height; }
};

class Sphere : public Shape
{
  double radius;
  public:
  Sphere(double radius):radius{radius}{}
  double getArea() override { return 4 * M_PI * radius * radius;  }
};

int main()
{
  string choice;

  cout << "please enter s for sphere or c for cylinder\n";

  cin >> choice;

  unique_ptr<Shape> shape;

  if ( choice == "s")
  {
    double radius;
    cout << "please enter radius\n";
    cin >> radius;
    shape = make_unique<Sphere>( radius );
  }
  else if ( choice == "c")
  {
    double radius, height;
    cout << "please enter radius and height\n";
    cin >> radius >> height;
    shape = make_unique<Cylinder>( radius, height);
  }
  else
  {
    cout << "invalid choice\n";
    return -1;
  }

  cout << "the area of the shape is " << shape->getArea() << "\n";
}
 
Share this answer
 
Comments
Stefan_Lang 24-Jun-21 12:31pm    
Almost perfect, except I'd rename the functions to getVolume() :-)

Have a 5 anyway
CPallini 24-Jun-21 14:51pm    
Check the formula I've used.
:-D
By the way, thank you!
Stefan_Lang 24-Jun-21 18:55pm    
Lol, didn't notice. Well done :-D
CPallini 25-Jun-21 1:59am    
Thank you again. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900