1

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

Lundin
  • 195,001
  • 40
  • 254
  • 396
Akash Sharma
  • 801
  • 1
  • 9
  • 19
  • 3
    You should avoid `using namespace std` to make sure you aren't getting one of the `std::abs` functions. And put your `abs` in a namespace to make sure you aren't getting any of the C standard library `abs` functions. – juanchopanza Nov 22 '13 at 10:39
  • 1
    Herbert Schildt is notorius in certain quarters for writing books that describe a language that is almost but not quite the same as C++. – john Nov 22 '13 at 10:40
  • I removed using namespace std; line and now its showing more errors. But I am using modern compiler so it should work with using namespace std – Akash Sharma Nov 22 '13 at 10:42
  • 4
    @AkashSharma No it shouldn't. Just because `using namespace std;` works in one program does not mean it will work in another. You should realise (it gets said often enough) that `using namespace std;` is dangerous, if you don't understand the danger then you should not use it. – john Nov 22 '13 at 10:43
  • @AkashSharma Please describe the new errors, I would guess that you need to replace `cout` with `std::cout`. – john Nov 22 '13 at 10:43
  • @john new error 'cout' : undeclared identifier – Akash Sharma Nov 22 '13 at 10:45
  • @AkashSharma Exactly, so replace `cout` with `std::cout`. Time to learn about namespaces I think, every C++ programmer should understand them, you can't get away with `using namespace std;` for ever. – john Nov 22 '13 at 10:46
  • [it works on a reasonable compiler](http://ideone.com/4ugu3H). It sounds like the book is giving you some bad advice. – juanchopanza Nov 22 '13 at 10:46
  • Here's [an example using your own namespace](http://ideone.com/KrDvIA). – juanchopanza Nov 22 '13 at 10:49
  • @john I tried using std::cout; but still its showing same errors. Is it problem with my software? I am using VS C++ 2010 – Akash Sharma Nov 22 '13 at 10:53
  • @john Screenshot of the errors what I am getting http://www.hlinker.com/stackoverflow/errors.png – Akash Sharma Nov 22 '13 at 10:56
  • [Related](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Griwes Nov 22 '13 at 10:59

2 Answers2

9

You should remove using namespace std, since there is already abs function in standard-library in cmath header, or you can wrap your own functions into some namespace.

So, since you have errors from math.h - you should use another names of functions, or trying to wrap functions into your own namespace.

ForEveR
  • 55,233
  • 2
  • 119
  • 133
0
#include<iostream>
using namespace std;
int f(int d)
{   return d<0.0 ?-d:d; }
double f(double d)
{  return d<0.0 ?-d:d; }`enter code here`
int main()
 {  cout<< f(2);
   cout<< f(2.3);    
    return 0;
 }