function templates

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

    #1

    function templates

    Hello,

    I want to implement a function template separated from the
    declaration:

    class X
    {
    public:
    template<class T> void operator << (T);
    };

    template<class T> void X::operator << (T t)
    {
    }

    I got the error:
    binary '<<' : 'class X' does not define this operator or a conversion
    to a type acceptable to the predefined operator

    When I implement the function inside the class X, everything is OK.
    How can this be solved, when the function "operator <<(T)" must be
    outside of the class X?

    Compiler is Microsoft Visual C++ for Windows CE 3.0.

    Regards,
    Helge
  • Gernot Frisch

    #2
    Re: function templates


    "Helge Kruse" <Helge.Kruse-nospam@gmx.net> schrieb im Newsbeitrag
    news:3ffa0259.0 411160108.23a94 850@posting.goo gle.com...[color=blue]
    > Hello,
    >
    > I want to implement a function template separated from the
    > declaration:
    >
    > class X
    > {
    > public:
    > template<class T> void operator << (T);
    > };
    >
    > template<class T> void X::operator << (T t)
    > {
    > }
    >
    > I got the error:
    > binary '<<' : 'class X' does not define this operator or a
    > conversion
    > to a type acceptable to the predefined operator
    >
    > When I implement the function inside the class X, everything is OK.
    > How can this be solved, when the function "operator <<(T)" must be
    > outside of the class X?
    >
    > Compiler is Microsoft Visual C++ for Windows CE 3.0.
    >
    > Regards,
    > Helge[/color]

    The template class definition must be in the header file. Or include
    an .inc file in the header.


    Comment

    • Rob Williscroft

      #3
      Re: function templates

      Helge Kruse wrote in news:3ffa0259.0 411160108.23a94 850@posting.goo gle.com
      in comp.lang.c++:

      [color=blue]
      > Compiler is Microsoft Visual C++ for Windows CE 3.0.[/color]

      That's you problem, your code is perfectly good C++, but
      some Microsoft compilers do like template members to be
      *defined* inside the body of the class.

      Workaround for non-inline:

      struct X;
      template < typename T > void op_shift_left( X *, T );

      struct X
      {
      template < typename T >
      void operator << ( T t )
      {
      op_shift_left( this, t );
      }
      };

      template < typename T >
      void op_shift_left( X *that, T t )
      {
      // real *non-inline* defenition here.
      }

      HTH.

      Rob.
      --

      Comment

      Working...