Correct syntax for class templates that declare friend functions

Discussion in 'C++' started by Oplec, Oct 28, 2003.

  1. Oplec

    Oplec Guest

    Hi, I am trying to figure out the correct syntax for declaring a friend
    function in a class template and then defining that member after the
    class. The code example below is what I am trying to get to work
    properly; it generates a link error.

    Thank you, Oplec.



    #include <cstdlib>
    #include <iostream>

    using namespace std;

    template<class C>
    class String {
    public:
    friend ostream& operator<<(ostream&, const String<C>&);
    };

    template<class C>
    ostream& operator<<(ostream& os, const String<C>& s) { return os; }

    int main()
    {
    String<char> s;
    cout << s << endl;

    system("PAUSE");
    }
     
    Oplec, Oct 28, 2003
    #1
    1. Advertising

  2. Oplec

    Silviu Minut Guest

    // Forward declaration of String, necessary for the fwd declaration
    // of operator<<() below
    template <class C>
    class String;

    // Fwd declaration
    template <class C>
    ostream & operator<<(ostream &, const String<C> &);


    // Class interface
    template <class C>
    class String
    {
    public:
    // Note the space between << and <
    friend ostream & operator<< <C> (ostream &, String<C> &);
    };

    // operator<<() implementation.
    template <class C>
    ostram & operator<<(ostream & OUT, const String<C> & S)
    {
    // Output S;
    }



    On Tue, 28 Oct 2003 00:29:39 +0000, Oplec wrote:

    > Hi, I am trying to figure out the correct syntax for declaring a friend
    > function in a class template and then defining that member after the
    > class. The code example below is what I am trying to get to work
    > properly; it generates a link error.
    >
    > Thank you, Oplec.
    >
    >
    >
    > #include <cstdlib>
    > #include <iostream>
    >
    > using namespace std;
    >
    > template<class C>
    > class String {
    > public:
    > friend ostream& operator<<(ostream&, const String<C>&);
    > };
    >
    > template<class C>
    > ostream& operator<<(ostream& os, const String<C>& s) { return os; }
    >
    > int main()
    > {
    > String<char> s;
    > cout << s << endl;
    >
    > system("PAUSE");
    > }
     
    Silviu Minut, Oct 28, 2003
    #2
    1. Advertising

  3. Oplec

    Oplec Guest

    Silviu Minut wrote:
    > // Forward declaration of String, necessary for the fwd declaration
    > // of operator<<() below
    > template <class C>
    > class String;
    >
    > // Fwd declaration
    > template <class C>
    > ostream & operator<<(ostream &, const String<C> &);
    >
    >
    > // Class interface
    > template <class C>
    > class String
    > {
    > public:
    > // Note the space between << and <
    > friend ostream & operator<< <C> (ostream &, String<C> &);
    > };
    >
    > // operator<<() implementation.
    > template <class C>
    > ostram & operator<<(ostream & OUT, const String<C> & S)
    > {
    > // Output S;
    > }


    I appreciate your trying to help me, but what you wrote gives a
    compile-time error for "friend ostream & operator<< <C> (ostream &,
    String<C> &);". The error just says "template-id". Note: I corrected the
    definition part where you wrote "ostram".

    Is there something else that was missed? I've never seen that notation
    before.

    Thanks, Oplec.
     
    Oplec, Oct 28, 2003
    #3
  4. Oplec

    DarkSpy Guest

    Oplec <> wrote in message news:<aCknb.80648$>...
    > Silviu Minut wrote:
    > > // Forward declaration of String, necessary for the fwd declaration
    > > // of operator<<() below
    > > template <class C>
    > > class String;
    > >
    > > // Fwd declaration
    > > template <class C>
    > > ostream & operator<<(ostream &, const String<C> &);
    > >
    > >
    > > // Class interface
    > > template <class C>
    > > class String
    > > {
    > > public:
    > > // Note the space between << and <
    > > friend ostream & operator<< <C> (ostream &, String<C> &);

    must be: friend ostream & operator<< <C> (ostream &, const String<C> &);

    > > };
    > >
    > > // operator<<() implementation.
    > > template <class C>
    > > ostram & operator<<(ostream & OUT, const String<C> & S)

    must be: ostream & operator<<(ostream & OUT, const String<C> & S)

    > > {
    > > // Output S;
    > > }

    >
     
    DarkSpy, Oct 28, 2003
    #4
  5. Oplec

    Silviu Minut Guest

    Certainly, I was sloppy, sorry. The forward declratation is

    >> > // Fwd declaration
    >> > template <class C>
    >> > ostream & operator<<(ostream &, const String<C> &);


    so there should also be const String<C> in

    friend ostream & operator<< <C> (ostream &, const String<C> &);

    The fwd declaration and the friend declaration must have the same
    prototype.
     
    Silviu Minut, Oct 28, 2003
    #5
  6. Oplec

    Oplec Guest

    Silviu Minut wrote:
    > Certainly, I was sloppy, sorry. The forward declratation is
    >
    >
    >>>>// Fwd declaration
    >>>>template <class C>
    >>>>ostream & operator<<(ostream &, const String<C> &);

    >
    >
    > so there should also be const String<C> in
    >
    > friend ostream & operator<< <C> (ostream &, const String<C> &);
    >
    > The fwd declaration and the friend declaration must have the same
    > prototype.
    >


    Hi,

    Adding the "const" corrected the previous error. I truly appreciate your
    helping me with this because I have never seen the <C> notation before
    the ()'s. Is there a specific name for it? I ask because I want to
    Google information about it so that I know what else to use it on.

    Thank you once again, Oplec.
     
    Oplec, Oct 28, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BigMan
    Replies:
    1
    Views:
    443
  2. freegnu
    Replies:
    2
    Views:
    343
    freegnu
    Oct 23, 2006
  3. =?gb2312?B?wfXquw==?=
    Replies:
    10
    Views:
    704
    Victor Bazarov
    Aug 1, 2007
  4. Replies:
    4
    Views:
    445
    Victor Bazarov
    Sep 30, 2007
  5. Andy Champ
    Replies:
    0
    Views:
    272
    Andy Champ
    Mar 25, 2008
Loading...

Share This Page