3

I was writing a program of function overloading in Visual C++ 2010 . Following is my code

// overload.cpp : Defines the entry point for the console application.

#include<Windows.h>
#include<iostream>
#include<conio.h>

using namespace std;

//abs is overloaded in 3 types
int abs(int i);
double abs(double d);
long abs(long  f);

void main()
{
    cout<<abs(-10)<<"\n";
    cout<<abs(-11.0)<<"\n";
    cout<<abs(-9L)<<"\n";
    getch();
}
int abs(int i)
{
    cout<<"using integer abs()\n";
    return i>0? -i:i;
}
double abs(double d)
{
    cout<<"using double abs()\n";
    return d>0? -d:d;
}
long abs (long l)
{
    cout<<"using long abs()\n";
    return l>0?-l:l;
}

I am having problems in double abs and long abs function that

1>c:\users\abc\documents\visual studio 2010\projects\overload\overload\overload.cpp(22): error C2084: function 'double abs(double)' already has a body
1>c:\users\abc\documents\visual studio 2010\projects\overload\overload\overload.cpp(26): error C2084: function 'long abs(long)' already has a body

Why this problem is coming? I have changed the compilation from c to c++ but recently I ran an other program for overloading,it worked.I don't know how? here is the code.

#include<iostream>
#include<cstdio>
#include<conio.h>
#include<cstring>
using namespace std;
void stradd(char*s1,char*s2);
void stradd(char*s1,int i);
void main()
{
    char str[80];
    strcpy(str,"hello");
    stradd(str,"there");
    cout<<str<<"\n";
    getch();
}
//concatenate a string with a "stringized "integer
void stradd(char*s1,int i)
{
    char temp[80];
    sprintf(temp,"%d",i);
    strcat(s1,temp);
}
//concatenate 2 strings
void stradd(char*s1,char *s2)
{
    strcat(s1,s2);
}

and output is hellothere

8
  • 9
    You probably have a name collision with std::abs. Try naming it absoluteValue or something else or remove using namespace std; and explicitely write std::cout. Commented Jul 18, 2013 at 10:31
  • when I changed the name of function then it worked but then what is the point of overloading.Also I removed namespace and wrote std::cout but still not working. Commented Jul 18, 2013 at 10:36
  • @MsFreedom911, use namespaces and you'll get rid of such a problems, it's so simply Commented Jul 18, 2013 at 10:42
  • @MsFreedom911 “what’s the point of overloading” – the point of overloading is to have different implementations for different argument types. In your code, you try to “overload” an existing function with the same argument type. That doesn’t work. Commented Jul 18, 2013 at 10:55
  • What's the question about second program? Commented Jul 18, 2013 at 11:30

4 Answers 4

5

Your problem comes from a header in which abs is declared for some types such as double. You're not allowed to have to functions with exactly the same header (that is, same return type, same name, same list of parameters, same qualifiers such as const).

There are two ways of avoiding this:

  1. Use the standard library: std::abs is good, you don't need to implement it yourself
  2. Naming the method absoluteValue or myAbs or whatever you like, but not abs

A third way, namely removing using namespace std does not work according to your comment. This is because you include Windows.h. This itself includes a bunch of headers, probably including math.h. This gives a method called abs in the global namespace. Better don't include Windows.h and include cmath if you need to. Then, abs is only declared in namespace std, hence you can call it with std::abs and is different from abs.

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

3 Comments

@MsFreedom911 The problem lies in the implementation of the header files on your computer. The author probably has a different one, so it's technically not his fault.
I have removed the windows header file ,but still no luck.I have written other program but this is working fine
@MsFreedom911, remove both #include<Windows.h> and using namespace std;
4

When overload resolution cannot select one function as the unique best match, the call is ambiguous. An ambiguous call produces a compilation error.

In std there is already an abs() with the following signature:

int abs (int n);

So while you try to overload it with double and long it results in ambiguity for the compiler.

If you're a beginner learning about coding i suggest you to use function names not defined in libraries (at least the ones you have included).

stefan have already given the solution to it:

Remove using namespace std; and explicitly write std::cout

OR

Re name your function to absoluteValue or something else

OR

Use explicit namespaces in function declaration and calls. (Not tested, though it should work)

Put your function inside a class or namespace.

Maybe this would provide you with a little insight (From SO).

EDIT:

The second question's overloaded functions stradd() is not defined in any other library. That is why no Compilation Errors. The following function signature in your code will result an error: char * strcat ( char * destination, const char * source )

5 Comments

so it means that we can't do overloading in c++ or I have to work on older compilers for c++.
@MsFreedom911 You can't replace an existing method with your own, if that's what you mean
@MsFreedom911, you can overload func(int param) with func(double param), but not with another func(int param). What compiler should choose to call if you have two equal functions?
To overload library function you need to use explicit namespace resolution
If you want error in your 2nd code, change your function signature to string char * strcat ( char * destination, const char * source );
2

Your primary problem is that you use global namespace. Just declare your function in your own namespace and all name collisions will be gone.
Let me explain why you're getting those compile-time errors.
Somewhere in the headers you included there are double abs(double) and long abs(long) functions. Then you're creating functions with the same signatures by your own. So compiler just don't know what to use when you'll call one of them - there are 2 pairs of equal functions. So it refuses to compile that, and you're getting those errors.
So you have 2 choices - hope that every time you'll want to create a function you will choose an unique name, or just create a namespace and your function names should be unique only to another functions in your namespace.
And it's not about overloading - void func(int i) overloads void func(float f), but void func(int i) overrides void func(int i). You can override superclass member functions in subclasses, but you cannot override standalone functions like abs().

Comments

0

just change abs function name with another. abs() is a keyword therefore it is showing errors.

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.