#include<iostream>
using namespace std;
int abs(int);
long abs(long int);
double abs(double);
int main()
{
int i=25,j;
long l=-100000,m;
double d=-12.34,e;
int abs(int ii)
{
return(ii>0?ii:ii*-1);
}
long abs(long ll)
{
return (ll>0?ll:ll*-1);
}
double abs(dd>0?dd:dd*-1);
}
\\please tell me if there is any error in this program.
compiler is showing the following error:
main.cpp: In function 'int main()':
main.cpp:13:8: error: call of overloaded 'abs(long int&)' is ambiguous
The standard library defines a function named abs. It would be hidden in namespace std, but you decided to make it visible by writing using namespace std.
Get rid of using namespace std, or name your own abs function as ::abs.
My personal preference is to leave using X to mean "I want argument-dependent lookup here".
For more reading, search for something like "using namespace std bad practice" on the web.
if you are going to write your own, I recommend flip the sign bit with & statement and try to force_inline it. This is not portable as the bit may move across system types, but it is at worst the same as the built in one for speed, at best it can actually beat it by a few cpu cycles.
The compiler I am using today does not like the double abs function.
But the code runs fine without it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<iostream>
usingnamespace std;
int main()
{
int i = 25, j;
long l = -100000, m;
double d = -12.34, e;
j = abs(i);
m = abs(l);
e = abs(d);
cout << endl << j << endl << m << endl << e << endl;
cin.get();
return 0;
}
Except now it does not work in the c++ shell. hmmm...