Functions

#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;

j=abs(i);
m=abs(l);
e=abs(d);
cout<<endl<<j<<endl<<m<<endl<<e<<endl;
return 0;
}

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.

You are having a similar problem as this guy:
http://www.cplusplus.com/forum/beginner/205160/#msg972411

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.

Also the last two lines of your code are nonsense
1
2
double abs(dd>0?dd:dd*-1);
{


You probably meant
1
2
3
4
double abs(double dd)
{ 
  return (dd > 0)? dd: -dd;
}


Edit:
although you probably want
1
2
3
4
double abs(double dd)
{ 
  return (dd < 0)? -dd : dd;
}
Last edited on
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.
Last edited on
The only function you need to define is the one for your double.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
 
 using namespace std;
 
 double abs(const double &);
 
 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;
 return 0;
 }

double abs(const double &dd)
 {
    return (dd>0) ? dd:dd*-1;
 }
Last edited on
The only function you need to define is the one for your double.

Your program is not portable.
http://coliru.stacked-crooked.com/a/6c55c06a65153ecf

See: http://www.cplusplus.com/forum/general/241498/#msg1073793
Last edited on
You are correct mbozzi.

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>

using namespace 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...
Last edited on
> Except now it does not work in the c++ shell. hmmm...

#include <cmath>
#include <cmath>


Pray tell Sir Borges, what tipped thee off that <cmath> should be includeth?
abs is a mathematical function defined in <cmath>.
http://www.cplusplus.com/reference/cmath/abs/
Well now you have done it Ganado...

If you need to include <cmath> to use abs, then why is it a problem to not use <cmath> but define your own abs?
Topic archived. No new replies allowed.