Hi,
You've run into a few problems.
1) you are using the variables names "two" and "three" multiple times. No good.
2) the std::greater is colliding with your greater. Put your greater in your own namespace. (This may be a GCC bug in the STL implementation. Or maybe std::greater is supposed to be in the global namespace, I'm not sure.)
3) the char const* versions of two and three won't compare as you'd expect, it's doing a pointer comparison. You need a template specialization to change the char const* situation.
HTH, --Eljay
- - - - - - - - - - - - - - - -
#include <iostream> #include <string> using std::cout; using std::string;
namespace learn_cpp {
template <typename T> // template parameter list T const& greater(T const& first, T const& second) { return (first > second) ? first : second; }
}
int main() { int a = 1, b = 2; cout << learn_cpp::greater(a, b) << '\n';
long c = 4L, d = 3L; cout << learn_cpp::greater(c, d) << '\n';
double e = 5.62, f = 3.48; cout << learn_cpp::greater(e, f) << '\n';
char g = 'A', h = 'a'; cout << learn_cpp::greater(g, h) << '\n';
string two1("two"), three1("three"); cout << learn_cpp::greater(two1, three1) << '\n';
char const* two2("two"), *three2("three"); cout << learn_cpp::greater(two2, three2) << '\n';
char const* two3("two"), *three3("three"); cout << learn_cpp::greater(two3, three3) << '\n';
return 0; }