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

12
  • 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. Commented Nov 22, 2013 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++. Commented Nov 22, 2013 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 Commented Nov 22, 2013 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. Commented Nov 22, 2013 at 10:43
  • @AkashSharma Please describe the new errors, I would guess that you need to replace cout with std::cout. Commented Nov 22, 2013 at 10:43

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I removed using namespace line and added new header <cmath> but no solution.
@AkashSharma you shouldn't include cmath. You must qualify cout with std:: prefix.
I tried std:: prefix but still same errors. hlinker.com/stackoverflow/errors.png
@AkashSharma ok. you should trying to wrap your functions into namespace, since there is abs in global ns.
@user2708138 it's not strange. It's not reglamented which header should be include in another header in standard library.
|
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;
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.