How to write output function for template classes?

Discussion in 'C++' started by Jayden Shui, Nov 16, 2011.

  1. Jayden Shui

    Jayden Shui Guest

    Hello All,

    I have a code like:

    template<class T, char open, char close>
    class Enclosed
    {
    public:
    Enclosed(T& t) : mT(t) {}

    friend ostream& operator<<(ostream& os, Enclosed const& e);

    private:
    T& mT;
    };

    template<class T, char open, char close>
    ostream& operator<<(ostream& os, Enclosed<T, open, close> const& e)
    {
    os << open << e.mT << close;
    return os;
    }

    When I test it by

    cout << Enclosed<int, '[', ']'>(5);

    The compiler said they can not find the operator << function. Would
    you please help me to look into it?

    Thank you for your kind help!

    Best regards,

    Jayden
    Jayden Shui, Nov 16, 2011
    #1
    1. Advertising

  2. On 11/16/2011 12:54 PM, Jayden Shui wrote:
    > Hello All,
    >
    > I have a code like:
    >
    > template<class T, char open, char close>
    > class Enclosed
    > {
    > public:
    > Enclosed(T& t) : mT(t) {}
    >
    > friend ostream& operator<<(ostream& os, Enclosed const& e);
    >
    > private:
    > T& mT;
    > };
    >
    > template<class T, char open, char close>
    > ostream& operator<<(ostream& os, Enclosed<T, open, close> const& e)
    > {
    > os<< open<< e.mT<< close;
    > return os;
    > }
    >
    > When I test it by
    >
    > cout<< Enclosed<int, '[', ']'>(5);
    >
    > The compiler said they can not find the operator<< function. Would
    > you please help me to look into it?


    -------------------------------------------- >8 cut here
    #include <iostream>
    using namespace std;

    template<class T, char open, char close>
    class Enclosed
    {
    public:
    Enclosed(T& t) : mT(t) {}

    friend ostream& operator<<(ostream& os, Enclosed const& e);

    private:
    T& mT;
    };

    template<class T, char open, char close>
    ostream& operator<<(ostream& os, Enclosed<T, open, close> const& e)
    {
    os << open << e.mT << close;
    return os;
    }

    int main()
    {
    cout << Enclosed<int, '[', ']'>(5);
    }
    -------------------------------------------- >8 cut here

    Comeau says

    "ComeauTest.c", line 11: warning: "std::eek:stream &operator<<(std::eek:stream
    &, const
    Enclosed<T, open, close> &)" declares a non-template function
    -- add
    <> to refer to a template instance
    friend ostream& operator<<(ostream& os, Enclosed const& e);
    ^

    "ComeauTest.c", line 26: error: no instance of constructor "Enclosed<T,
    open,
    close>::Enclosed [with T=int, open='[', close=']']" matches the
    argument list
    The argument types that you used are: (int)
    cout << Enclosed<int, '[', ']'>(5);
    ^

    You declare a non-template operator<< as the friend of your class
    template and then you define a template function... See FAQ 35.16.

    BTW, you need to make your Enclosed take a 'const T&' if you want to use
    it with the literal like '5'.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 16, 2011
    #2
    1. Advertising

  3. Jayden Shui

    Jayden Shui Guest

    On Nov 16, 1:19 pm, Victor Bazarov <> wrote:
    > On 11/16/2011 12:54 PM, Jayden Shui wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > Hello All,

    >
    > > I have a code like:

    >
    > > template<class T, char open, char close>
    > > class Enclosed
    > > {
    > > public:
    > >      Enclosed(T&  t) : mT(t) {}

    >
    > >      friend ostream&  operator<<(ostream&  os, Enclosed const&  e);

    >
    > > private:
    > >      T&  mT;
    > > };

    >
    > > template<class T, char open, char close>
    > > ostream&  operator<<(ostream&  os, Enclosed<T, open, close>  const&  e)
    > > {
    > >      os<<  open<<  e.mT<<  close;
    > >      return os;
    > > }

    >
    > > When I test it by

    >
    > > cout<<  Enclosed<int, '[', ']'>(5);

    >
    > > The compiler said they can not find the operator<<  function. Would
    > > you please help me to look into it?

    >
    > -------------------------------------------- >8 cut here
    > #include <iostream>
    > using namespace std;
    >
    > template<class T, char open, char close>
    > class Enclosed
    > {
    > public:
    >      Enclosed(T& t) : mT(t) {}
    >
    >      friend ostream& operator<<(ostream& os, Enclosed const& e);
    >
    > private:
    >      T& mT;
    >
    > };
    >
    > template<class T, char open, char close>
    > ostream& operator<<(ostream& os, Enclosed<T, open, close> const& e)
    > {
    >      os << open << e.mT << close;
    >      return os;
    >
    > }
    >
    > int main()
    > {
    >     cout << Enclosed<int, '[', ']'>(5);}
    >
    > -------------------------------------------- >8 cut here
    >
    > Comeau says
    >
    > "ComeauTest.c", line 11: warning: "std::eek:stream &operator<<(std::eek:stream
    > &, const
    >            Enclosed<T, open, close> &)" declares a non-template function
    > -- add
    >            <> to refer to a template instance
    >        friend ostream& operator<<(ostream& os, Enclosed const& e);
    >                        ^
    >
    > "ComeauTest.c", line 26: error: no instance of constructor "Enclosed<T,
    > open,
    >            close>::Enclosed [with T=int, open='[', close=']']" matches the
    >            argument list
    >              The argument types that you used are: (int)
    >       cout << Enclosed<int, '[', ']'>(5);
    >               ^
    >
    > You declare a non-template operator<< as the friend of your class
    > template and then you define a template function...  See FAQ 35.16.
    >
    > BTW, you need to make your Enclosed take a 'const T&' if you want to use
    > it with the literal like '5'.
    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask


    Thank you very much! It solved my question.

    Best regards,

    Jayden
    Jayden Shui, Nov 16, 2011
    #3
    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. Robert Allan Schwartz
    Replies:
    1
    Views:
    395
    Victor Bazarov
    Aug 9, 2004
  2. Jim West
    Replies:
    3
    Views:
    757
    Jim West
    Oct 7, 2004
  3. Replies:
    5
    Views:
    343
    James Kanze
    Apr 27, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    768
    Thomas J. Gritzan
    Oct 26, 2008
  5. Replies:
    2
    Views:
    800
Loading...

Share This Page