How to define inserter or extractor for a class template

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laikon

    How to define inserter or extractor for a class template

    this question is about how to define friend functions for a class
    template.
    the following is an example.

    template <typename T>
    class Array
    {
    private:
    T* parr;
    int sz;

    friend ostream& operator << (ostream& os, const Array<T>& rhs);
    };

    template <class T>
    ostream& operator << (ostream& os, const Array<T>& rhs)
    {
    for (int i = 0; i < rhs.sz; ++i)
    os << rhs.parr[i] << "\t";
    return os;
    }

    these codes work well in VC++ 6.0, while linking errors are given in VC
    ++ 2005.
  • Barry

    #2
    Re: How to define inserter or extractor for a class template

    On Oct 8, 2:57 pm, laikon <lai...@gmail.c omwrote:
    this question is about how to define friend functions for a class
    template.
    the following is an example.
    >
    template <typename T>
    class Array
    {
    private:
            T* parr;
            int sz;
    >
            friend ostream& operator << (ostream& os, const Array<T>&rhs);
    friend ostream& operator<< <T(ostream& os, const Array<T>& rhs);
    >
    };
    >
    template <class T>
    ostream& operator << (ostream& os, const Array<T>& rhs)
    {
            for (int i = 0; i < rhs.sz; ++i)
                    os << rhs.parr[i] << "\t";
            return os;
    >
    }
    >
    these codes work well in VC++ 6.0, while linking errors are given in VC
    ++ 2005.
    --
    Best Regards
    Barry

    Comment

    • Barry

      #3
      Re: How to define inserter or extractor for a class template

      On Oct 8, 3:19 pm, Barry <dhb2...@gmail. comwrote:
      On Oct 8, 2:57 pm, laikon <lai...@gmail.c omwrote:
      >
      this question is about how to define friend functions for a class
      template.
      the following is an example.
      >
      template <typename T>
      class Array
      {
      private:
              T* parr;
              int sz;
      >
              friend ostream& operator << (ostream& os, const Array<T>& rhs);
      >
      friend ostream& operator<< <T(ostream& os, const Array<T>& rhs);
      >
      Note that this is only a work around for VC2005.
      According to 10.5.3
      We should forward declaring "template operator<<", which requires
      forward
      declaration of "template class Array".
      So the code looks like this:

      template <typename T>
      class Array;

      template <typename T>
      ostream& operator<< (ostream&, Array<Tconst&);

      template <typename T>
      class Array {
      ...

      friend
      ostream& operator<< <T(ostream&, Array const&); // Array<Tis
      also fine
      };

      template <typename T>
      ostream& operator<< (ostream& ostrm, Array<Tconst& a) {
      ...
      }
      >
      >
      };
      >
      template <class T>
      ostream& operator << (ostream& os, const Array<T>& rhs)
      {
              for (int i = 0; i < rhs.sz; ++i)
                      os << rhs.parr[i] << "\t";
              return os;
      >
      }
      >
      these codes work well in VC++ 6.0, while linking errors are given in VC
      ++ 2005.
      >
      --
      Best Regards
      Barry

      Comment

      • James Kanze

        #4
        Re: How to define inserter or extractor for a class template

        On Oct 8, 8:57 am, laikon <lai...@gmail.c omwrote:
        this question is about how to define friend functions for a
        class template.
        It's difficult:-).
        the following is an example.
        template <typename T>
        class Array
        {
        private:
        T* parr;
        int sz;
        friend ostream& operator << (ostream& os, const Array<T>& rhs);
        Note that this declares a non-template operator<< as a friend.
        That's probably not what you want.
        };
        template <class T>
        ostream& operator << (ostream& os, const Array<T>& rhs)
        {
        for (int i = 0; i < rhs.sz; ++i)
        os << rhs.parr[i] << "\t";
        return os;
        }
        these codes work well in VC++ 6.0, while linking errors are
        given in VC ++ 2005.
        If you have a declaration of the operator<<, as a template,
        before you define the class, I think it should work. In
        practice, I've found it more convenient to only use inline
        friends, so the problem doesn't come up. Something like:

        template< typename T >
        class Array
        {
        // ...
        public:
        void print( std::ostream& dest ) const ;
        friend std::ostream&
        operator<<( std::ostream& dest,
        Array< T const& array )
        {
        array.print( dest ) ;
        return *this ;
        }
        } ;

        The friend may be a non-template function, but it doesn't
        matter, since I don't have to declare it anywhere else.

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        Working...