public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* String and Char in function template
@ 2004-08-31 22:06 learning c++
  0 siblings, 0 replies; only message in thread
From: learning c++ @ 2004-08-31 22:06 UTC (permalink / raw)
  To: gcc-help

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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2004-08-31 20:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-31 22:06 String and Char in function template learning c++

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).