I am trying to run an example of Function Overloading in C++. But Its showing me following error
function.cpp(21): error C2084: function 'double abs(double)' already has a body
include\math.h(495) : see previous definition of 'abs'
function.cpp(26): error C2084: function 'long abs(long)' already has a body
include\stdlib.h(467) : see previous definition of 'abs'
Program
#include<iostream>
using namespace std;
int abs(int i);
double abs(double d);
long abs(long l);
int main()
{
cout << abs(-10);
cout << abs(-20.2);
cout << abs(-30L);
return 0;
}
int abs(int i)
{
cout << "Using int \n";
return i<0 ? -i:i;
}
double abs(double d)
{
cout << "Using Double \n";
return d<0.0 ?-d:d;
}
long abs(long l)
{
cout << "Using Long\n";
return l<0?-l:l;
}
I have copied the same code as given in book C++ Complete Reference , Fourth Edition by Herbert Schildt
using namespace stdto make sure you aren't getting one of thestd::absfunctions. And put yourabsin a namespace to make sure you aren't getting any of the C standard libraryabsfunctions.using namespace std;works in one program does not mean it will work in another. You should realise (it gets said often enough) thatusing namespace std;is dangerous, if you don't understand the danger then you should not use it.coutwithstd::cout.