I have the following code
Code:
```//illustrates overloading functions
#include <iostream>
using namespace std;

class rational{

private:
long a, q;
enum { BIG = 100};

public:
rational(int n = 0) : a(n), q(1) {}

rational(int i, int j) : a(i), q(j) {}

rational(double r) : a(static_cast<long> (r * BIG) ), q(BIG) {}

void print() const { cout << a << "/" << q; }

operator double() { return static_cast<double>(a) / q; }
};

//functions
inline int greater(int i, int j){
return ( i > j ? i : j);
}

inline double greater(double x, double y){
return (x > y ? x : y);
}

inline rational greater(rational w, rational z){
return (w > z ? w : z);
}

int main(void){

int i = 10, j = 5;
float x = 7.0;
double y = 14.5;
rational w(10), z(3.5), zmax;

cout <<"\ngreater(" << i << "," << j << ") = " << greater(i,j);
cout <<"\ngreater(" << x << "," << y << ") = " << greater(x,y);
cout <<"\ngreater(" << i << ",";
z.print();
cout <<") = " << greater(static_cast<rational>(i), z);
zmax = greater(w,z);
cout <<"\ngreater(";
w.print();
cout << ", ";
z.print();
cout << ") = ";
zmax.print();
cout << endl;

return 0;
}```

and getting the following errors.
Code:
```\$ g++ rational.cpp -o rational
rational.cpp: In function ‘int main()’:
rational.cpp:43:52: error: reference to ‘greater’ is ambiguous
rational.cpp:32:17: error: candidates are: rational greater(rational, rational)
rational.cpp:28:15: error:                 double greater(double, double)
rational.cpp:24:12: error:                 int greater(int, int)
In file included from /usr/include/c++/4.7/string:50:0,
from /usr/include/c++/4.7/bits/locale_classes.h:42,
from /usr/include/c++/4.7/bits/ios_base.h:43,
from /usr/include/c++/4.7/ios:43,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from rational.cpp:2:
/usr/include/c++/4.7/bits/stl_function.h:224:12: error:                 template<class _Tp> struct std::greater
rational.cpp:44:52: error: reference to ‘greater’ is ambiguous
rational.cpp:32:17: error: candidates are: rational greater(rational, rational)
rational.cpp:28:15: error:                 double greater(double, double)
rational.cpp:24:12: error:                 int greater(int, int)
In file included from /usr/include/c++/4.7/string:50:0,
from /usr/include/c++/4.7/bits/locale_classes.h:42,
from /usr/include/c++/4.7/bits/ios_base.h:43,
from /usr/include/c++/4.7/ios:43,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from rational.cpp:2:
/usr/include/c++/4.7/bits/stl_function.h:224:12: error:                 template<class _Tp> struct std::greater
rational.cpp:47:19: error: reference to ‘greater’ is ambiguous
rational.cpp:32:17: error: candidates are: rational greater(rational, rational)
rational.cpp:28:15: error:                 double greater(double, double)
rational.cpp:24:12: error:                 int greater(int, int)
In file included from /usr/include/c++/4.7/string:50:0,
from /usr/include/c++/4.7/bits/locale_classes.h:42,
from /usr/include/c++/4.7/bits/ios_base.h:43,
from /usr/include/c++/4.7/ios:43,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from rational.cpp:2:
/usr/include/c++/4.7/bits/stl_function.h:224:12: error:                 template<class _Tp> struct std::greater
rational.cpp:48:9: error: reference to ‘greater’ is ambiguous
rational.cpp:32:17: error: candidates are: rational greater(rational, rational)
rational.cpp:28:15: error:                 double greater(double, double)
rational.cpp:24:12: error:                 int greater(int, int)
In file included from /usr/include/c++/4.7/string:50:0,
from /usr/include/c++/4.7/bits/locale_classes.h:42,
from /usr/include/c++/4.7/bits/ios_base.h:43,
from /usr/include/c++/4.7/ios:43,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from rational.cpp:2:
/usr/include/c++/4.7/bits/stl_function.h:224:12: error:                 template<class _Tp> struct std::greater```
Seems to be saying I can't call the greater function? Just learing about function overloading so any help would be appreciated.

2. just replace
Code:
`using namespace std;`
with
Code:
```using  std::cout;
using  std::endl;```
and the compiler will know what to call.
It's never a good idea to give your own functions the same name like standard functions.
Kurt

3. This is why the using namespace std; clause is not recommended. You could also put your greater() functions into your own namespace then properly scope your namespace using the scope resolution operator::

Code:
```namespace hexDump
{
//functions
inline int greater(int i, int j) {
return ( i > j ? i : j);
}

inline double greater(double x, double y) {
return (x > y ? x : y);
}

inline rational greater(rational w, rational z) {
return (w > z ? w : z);
}

} // end of namespace```
Then when using your greater() functions:

Code:
`zmax = hexDump::greater(w,z);`
Of course you could always rename your functions to something else as well.

Jim

4. Excellent thanks guys. So what would u call this? A scoping or namespace error?

Also when was greater() added, id think my book would have known and anticipated this.

5. So what would u call this? A scoping or namespace error?
A name clash. These name clashes are the reasons namespaces were introduced.

Also when was greater() added, id think my book would have known and anticipated this.
Does your book use the "using namespace std;" clause or did you add that.

Jim

6. Originally Posted by jimblumberg
A name clash. These name clashes are the reasons namespaces were introduced.
Hmmm...you'd think I'd at least get a decent error message then. Something like "name clashes with std::greater" or something to that effect...

Does your book use the "using namespace std;" clause or did you add that.
Jim
It uses the "using namespace std;" convention. The book is C++ By Dissection: Ira Pohl: 9780201787337: Amazon.com: Books Not a big fan of it though, but I started it and I have mild OCD so I want to finish it and then move on to Prata's C++ Primer Plus and Accelerated C++.

I'm glad I got this problem though, this was good to know info.

7. > Hmmm...you'd think I'd at least get a decent error message then. Something like "name clashes with std::greater" or something to that effect...
You mean like this?
Code:
```/usr/include/c++/4.7/bits/stl_function.h:224:12: error:                 template<class _Tp> struct std::greater
rational.cpp:48:9: error: reference to ‘greater’ is ambiguous
rational.cpp:32:17: error: candidates are: rational greater(rational, rational)
rational.cpp:28:15: error:                 double greater(double, double)
rational.cpp:24:12: error:                 int greater(int, int)```

8. Originally Posted by Salem
> Hmmm...you'd think I'd at least get a decent error message then. Something like "name clashes with std::greater" or something to that effect...
You mean like this?
Code:
```/usr/include/c++/4.7/bits/stl_function.h:224:12: error:                 template<class _Tp> struct std::greater
rational.cpp:48:9: error: reference to ‘greater’ is ambiguous
rational.cpp:32:17: error: candidates are: rational greater(rational, rational)
rational.cpp:28:15: error:                 double greater(double, double)
rational.cpp:24:12: error:                 int greater(int, int)```
Hahahha yes, like that. Haven't gotten to templates and such yet so that was yiddish to me. Thanks though.

9. Originally Posted by hex_dump
Hahahha yes, like that. Haven't gotten to templates and such yet so that was yiddish to me. Thanks though.
Even if you don't seem to understand a compiler error, always read it.
Once I had a nasty template error and naturally, skipped it and just posted it here!
It turned out that the error message *literally* said what I had to do to get rid of it.

10. Originally Posted by hex_dump
Hmmm...you'd think I'd at least get a decent error message then. Something like "name clashes with std::greater" or something to that effect...
But it's not technically a clash. You brought in more symbols with the name greater from the std namespace, which peacefully coexists with what you wrote. But when you call it, the compiler doesn't know which one you meant, hence ambiguous.

11. If it's not a name clash then what is it?

12. I don't know the exact terminology, I was just responding to why the compiler shouldn't say "omg, your Y clashes with X."

13. Originally Posted by whiteflags
If it's not a name clash then what is it?
The correct terminology is "ambiguity" or (as used by the compiler in the original post) "ambiguous". The error message meant that, in the code which called the function, there is more than one possible candidate function to be called, and the compiler has no reason to prefer one over the other.

The compiler is correct in using that terminology, because that is the terminology used by the C++ standard. The standard is not using that terminology in some unusual manner either - in the English language, "ambiguity" is a noun meaning "doubtfulness or uncertainty of meaning or intention". The word "ambiguous" is an adjective meaning "open to or having several possible meanings or interpretations" or "of doubtful or uncertain nature".

It is often tiring in forums to see members debating the terminology for something that the standard defines. People seem to delight in introducing new terminology that - they claim - is clearer than the standard but, in reality, simply obfuscates what is going on.

Popular pages Recent additions