This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

String and Char in function template


Hi, Everyone:

I have a short code about function template like this:

#include <iostream>
#include <string>
using std::cout;

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 << greater(a, b) << '\n';
   long c = 4L, d = 3L;
   cout << greater(c, d) << '\n';
   double e = 5.62, f = 3.48;
   cout << greater(e, f) << '\n';
   char g = 'A', h = 'a';
   cout << greater(g, h) << '\n';
   std::string two("two"), three("three");
   cout << greater(two, three) << '\n';
   char const *two("two"), *three("three");
   cout << greater(two, three) << '\n';
   char const *two("two"), *three("three");
   cout << greater(two, three) << '\n';
   return 0;
}

when I compile it, the errors are the following:
/v/dunix51_alpha/gnu/gcc/lib/gcc-lib/alphaev5-dec-osf5/3.3/include/c++/bits/stl_function.h: In
function `int main()':
/v/dunix51_alpha/gnu/gcc/lib/gcc-lib/alphaev5-dec-osf5/3.3/include/c++/bits/stl_function.h:188: error: `
template<class _Tp> struct std::greater' is not a function,
greater.cpp:7: error: conflict with `template<class T> const T& greater(const
T&, const T&)'
greater.cpp:22: error: in call to `greater'
greater.cpp:23: error: conflicting types for `const char*two'
greater.cpp:21: error: previous declaration as `std::string two'
greater.cpp:23: error: conflicting types for `const char*three'
greater.cpp:21: error: previous declaration as `std::string three'
greater.cpp:23: warning: statement with no effect
greater.cpp:23: confused by earlier errors, bailing out


What is wrong with my code? In addition, I hope to know something about "const" in "char const *two("two"), *three("three");" and the difference between "using std::cout;" and "using namespace std;"

Thanks in advance!

_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*. http://join.msn.com/?page=features/featuredemail



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]