problem with templates

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

    #1

    problem with templates

    Hi,

    I'm working with templates, but I have a syntax problem I don't know
    how to fix.

    I built a small example, so maybe you can tell me how to write
    correctly this code.

    I have 2 files : template.H and template.C

    Template.H :

    #ifndef _TEMPLATE_H_739 832983298
    #define _TEMPLATE_H_739 832983298

    template <class T>
    class MyTemplate
    {
    class OtherClass
    {
    int foo;
    };
    T value;

    public:
    OtherClass changeValue(Oth erClass newValue);
    };

    #endif

    Template.C :

    #include <template.H>

    template <class T>
    MyTemplate< T >::OtherClass MyTemplate< T >::changeValue( MyTemplate< T
    >::OtherClass newValue)
    {
    return newValue;
    }

    Here is my problem, I don't know how to make the definition of method
    changeValue().
    If it returns and receives a simple parameter, like int, I have no
    problem, but the return type and parameter type is another class
    defined inside MyTemplate.

    Working in Solaris, compiling with CC I get the following warning.
    "/home/usuarios/lrojas/engine2.5/src/template.C", line 4: Warning
    (Anachronism): Type names qualified by template parameters require
    "typename".
    "/home/usuarios/lrojas/engine2.5/src/template.C", line 4: Warning
    (Anachronism): Type names qualified by template parameters require
    "typename".

    In Linux/g++ it doesn't even compile, it generates an error instead of
    a warning.

    template.C:4: error: expected constructor, destructor, or type
    conversion before "MyTemplate "

    What is the right syntax to define methods for MyTemplate, when I need
    to use a parameter type defined inside MyTemplate, like "OtherClass " ?

    I've been reading some tutorials on templates in internet, but couldn't
    find this situation.

    Thanks for any advice!

    Luis

  • Victor Bazarov

    #2
    Re: problem with templates

    kalki70 wrote:
    I'm working with templates, but I have a syntax problem I don't know
    how to fix.
    >
    I built a small example, so maybe you can tell me how to write
    correctly this code.
    >
    I have 2 files : template.H and template.C
    >
    Template.H :
    >
    #ifndef _TEMPLATE_H_739 832983298
    Any names that start with an underscore and a capital letter are
    reserved by the implementation. IOW, do _not_ use them, you are
    not allowed to. Why do you think you need the leading underscore
    here?
    #define _TEMPLATE_H_739 832983298
    >
    template <class T>
    class MyTemplate
    {
    class OtherClass
    {
    int foo;
    };
    T value;
    >
    public:
    OtherClass changeValue(Oth erClass newValue);
    };
    >
    #endif
    >
    Template.C :
    >
    #include <template.H>
    >
    template <class T>
    MyTemplate< T >::OtherClass MyTemplate< T >::changeValue( MyTemplate< T
    >::OtherClass newValue)
    Try not to post wrapped text that starts with ">" or a similar character
    used in quoting.
    {
    return newValue;
    }
    >
    Here is my problem, I don't know how to make the definition of method
    changeValue().
    You almost did it right. What you need is to place 'typename' in two
    places in the "head" of that function:

    template<class T>
    typename MyTemplate<T>:: Otherclass // <- there
    MyTemplate<T>:: changeValue(
    typename MyTemplate<T>:: OtherClass newValue) // <- there
    {
    return newValue;
    }
    If it returns and receives a simple parameter, like int, I have no
    problem, but the return type and parameter type is another class
    defined inside MyTemplate.
    >
    Working in Solaris, compiling with CC I get the following warning.
    "/home/usuarios/lrojas/engine2.5/src/template.C", line 4: Warning
    (Anachronism): Type names qualified by template parameters require
    "typename".
    "/home/usuarios/lrojas/engine2.5/src/template.C", line 4: Warning
    (Anachronism): Type names qualified by template parameters require
    "typename".
    >
    In Linux/g++ it doesn't even compile, it generates an error instead of
    a warning.
    >
    template.C:4: error: expected constructor, destructor, or type
    conversion before "MyTemplate "
    >
    What is the right syntax to define methods for MyTemplate, when I need
    to use a parameter type defined inside MyTemplate, like "OtherClass " ?
    You need 'typename' there.
    I've been reading some tutorials on templates in internet, but
    couldn't find this situation.
    Read the FAQ.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • kalki70

      #3
      Re: problem with templates

      THANKS!!!

      Victor Bazarov wrote:
      kalki70 wrote:
      I'm working with templates, but I have a syntax problem I don't know
      how to fix.

      I built a small example, so maybe you can tell me how to write
      correctly this code.

      I have 2 files : template.H and template.C

      Template.H :

      #ifndef _TEMPLATE_H_739 832983298
      >
      Any names that start with an underscore and a capital letter are
      reserved by the implementation. IOW, do _not_ use them, you are
      not allowed to. Why do you think you need the leading underscore
      here?
      >
      #define _TEMPLATE_H_739 832983298

      template <class T>
      class MyTemplate
      {
      class OtherClass
      {
      int foo;
      };
      T value;

      public:
      OtherClass changeValue(Oth erClass newValue);
      };

      #endif

      Template.C :

      #include <template.H>

      template <class T>
      MyTemplate< T >::OtherClass MyTemplate< T >::changeValue( MyTemplate< T
      >::OtherClass newValue)
      >
      Try not to post wrapped text that starts with ">" or a similar character
      used in quoting.
      >
      {
      return newValue;
      }

      Here is my problem, I don't know how to make the definition of method
      changeValue().
      >
      You almost did it right. What you need is to place 'typename' in two
      places in the "head" of that function:
      >
      template<class T>
      typename MyTemplate<T>:: Otherclass // <- there
      MyTemplate<T>:: changeValue(
      typename MyTemplate<T>:: OtherClass newValue) // <- there
      {
      return newValue;
      }
      >
      If it returns and receives a simple parameter, like int, I have no
      problem, but the return type and parameter type is another class
      defined inside MyTemplate.

      Working in Solaris, compiling with CC I get the following warning.
      "/home/usuarios/lrojas/engine2.5/src/template.C", line 4: Warning
      (Anachronism): Type names qualified by template parameters require
      "typename".
      "/home/usuarios/lrojas/engine2.5/src/template.C", line 4: Warning
      (Anachronism): Type names qualified by template parameters require
      "typename".

      In Linux/g++ it doesn't even compile, it generates an error instead of
      a warning.

      template.C:4: error: expected constructor, destructor, or type
      conversion before "MyTemplate "

      What is the right syntax to define methods for MyTemplate, when I need
      to use a parameter type defined inside MyTemplate, like "OtherClass " ?
      >
      You need 'typename' there.
      >
      I've been reading some tutorials on templates in internet, but
      couldn't find this situation.
      >
      Read the FAQ.
      >
      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask

      Comment

      Working...