0

C++ - I'm supposed to compute the area of two shapes by overloading the area() function and the way I want to solve the problem is by passing the values of the constants as default arguments to both the functions, as the problem statement asks me to write one for each shape. But when I try to compile the code, I get the error message "undefined reference" for both the functions. Now I could straight away look it up on the web, but I want to know where am I going wrong.

I'm using Ubuntu 16.04.

#include<iostream>
#include<math.h>

using namespace std;

double area(int r, float pi=3.14);    //prototype for calculating the area of circle
double area(int l, int h, float a=0.5);    //prototype for area of triangle

int main()
{
    int l, r, h;

    cout<<"Enter the radius of the circle\n";    //accepting radius
    cin>>r;

    cout<<"Enter length of one side and height of triangle\n";    //accepting length and height
    cin>>l>>h;

    cout<<"Area of circle is = "<<area(r)<<"\n\n";
    cout<<"Area of triangle is = "<<area(l, h)<<"\n";
}

double area(int r)    //declaration for circle, passing radius accepted and pi as default parameter
{
    float pi;

    double z = r*r*pi;

    return z;
}

double area(int l, int h)    //declaration for triangle passing l and b accepted, and 0.5 as default parameter
{
    float a;

    double z = l*h*a;

    return z;
}

the error message:

X

James Z
  • 12,209
  • 10
  • 24
  • 44
harsh
  • 1
  • 3
  • Why would you take pi as argument, do you expect another value? – YSC Sep 28 '18 at 12:02
  • Check the correct syntax for default argument, this is not how it works. Find a good C++ book and learn C++. Guessing C++ is a terrible idea. – YSC Sep 28 '18 at 12:03
  • I was trying to learn how to pass default arguments. Which C++ book do you recommend? – harsh Sep 28 '18 at 12:09
  • 1
    Learning is good. But don't try and _guess_. It might appear to work and you'll end up thinking you know something, but it will be wrong. Start with a good book. – YSC Sep 28 '18 at 12:11
  • [Here's a list of generally recommended C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Sep 28 '18 at 12:19
  • the SO way of marking a question as solved is to accept one of the answers (by clicking on the checkmark next to it) – 463035818_is_not_an_ai Sep 28 '18 at 12:30

1 Answers1

3

You declare one area function with two arguments, and another with three. But you never define those functions! Your area function definitions are for a function with one argument, and one with two arguments.

The definitions needs all arguments, matching those in the declarations:

double area(int r, float pi)    //definition for circle, passing radius accepted and pi as default parameter
{
    ...
}

double area(int l, int h, float a)    //definition for triangle passing l and b accepted, and 0.5 as default parameter
{
    ...
}

Note that the default values are not needed in the definitions of the functions, as they're already in the declarations.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621