1

Can someone help me understand why these 2 function calls are ambiguous:

int             greater(int i, int j)           { return ( i > j ? i : j); }
double          greater(double x, double y)     { return ( x > y ? x : y); }

int main(){
int          i = 10,         j = 5;
double       x = 7.7,        y = 14.5;
cout << greater(i, j);
cout << greater(x, y);
}

The exact error from g++ is:

reference to ‘greater’ is ambiguous cout << greater(i, j);

This is from Prof. Pohl's excellent book on C++ for C Programmers. I'm compiling with g++ (GCC) 4.9.2. thank you, Mainecat

LACat
  • 21
  • 2

1 Answers1

8

With your usage, those two functions are not ambiguous.

You're getting an error because of this (assumed) line:

using namespace std;

This line makes your function ambiguous because std::greater is now a candidate.

Avoid using namespace std;

Community
  • 1
  • 1
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180