I have a simple code.... error: call of overloaded âabs(int&)â is ambiguous

heres the code: (function)
bool ValidMoveCheck (int row, int row1, int column, int column1)
{
bool check = false;
if (abs(row) - (row1) == 1)
{
check = true;
return check;
}
else if (abs(column) - (column1) == 1)
{
check = true;
return check;
}
else
{
cout << "Please enter valid row number/column number." << endl;
check = false;
return check;
}
}

the problem has to be about abs....i dunno why its not working....thanks in advance
Last edited on
Not sure what headers etc you include but IIRC in the c-runtime abs() function has a prototype that takes a double value:

double abs(double);

maybe that is the problem?

for int's you could just do a small function

1
2
3
4
int myabs(int n) 
{ 
   return (n < 0 )? -n : n;
}


or since you always do the same cmp

1
2
3
4
bool isDistanceOne( int x, int y )
{
   return ( (x < 0)?-x:x - (y < 0)?-y:y );
}


then your code would look like:

1
2
3
4
if ( isDistanceOne( row, row1 ) )
{
 ...
}


hth
i just want it to take the absolute value of 2 inputs that the user types....
Not sure why you are getting the error. I'm in Windows right now w/no access to compiler, and it's too late for me to look it up.

std::max( x, -x ) would give you the absolute value.
Topic archived. No new replies allowed.