function template compilation error.

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

    function template compilation error.

    Hi,
    I'm using the g++ compiler to compile this on SunOS 5.8.
    I get compilation errors when I use A* as return type but none when I
    used void .
    why is this ?
    regards,
    Aman.
    ----------errors -------------------

    :6: syntax error before `*'
    :20: syntax error before `*'
    :20: `T' was not declared in this scope
    :20: template argument 1 is invalid
    :20: `T' was not declared in this scope
    :20: parse error before `)'
    :21: ANSI C++ forbids declaration `function' with no type

    ----------- Code --------------------

    #include <iostream>
    using namespace std ;
    template <typename T>
    class test {
    public :
    A* function(T) ; // using void as return type compiles fine .
    private :
    struct A{
    T somedata ;
    int i ;
    } ;

    };
    template <typename T>
    A* test<T> :: function(T in) // using void as return type works fine.
    {
    }



    --
    Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
  • Jonathan Mcdougall

    #2
    Re: function template compilation error.

    > Hi,[color=blue]
    > I'm using the g++ compiler to compile this on SunOS 5.8.
    > I get compilation errors when I use A* as return type but none when I
    > used void .
    > why is this ?
    > regards,
    > Aman.
    > ----------errors -------------------
    >
    > :6: syntax error before `*'
    > :20: syntax error before `*'
    > :20: `T' was not declared in this scope
    > :20: template argument 1 is invalid
    > :20: `T' was not declared in this scope
    > :20: parse error before `)'
    > :21: ANSI C++ forbids declaration `function' with no type
    >
    > ----------- Code --------------------
    >
    > #include <iostream>
    > using namespace std ;
    > template <typename T>
    > class test {
    > public :
    > A* function(T) ; // using void as return type compiles fine .
    > private :
    > struct A{
    > T somedata ;
    > int i ;
    > } ;
    >
    > };[/color]

    Try to put the struct definition before it is used in the class.

    [color=blue]
    > template <typename T>
    > A* test<T> :: function(T in) // using void as return type works fine.[/color]

    Should be

    template <typename T>
    test<T>::A* test<T> :: function(T in)


    Jonathan


    Comment

    • Aman

      #3
      Re: function template compilation error.

      This works fine now , thank you very much .

      what's the general rule on using qualification for the types ?
      when ever a struct etc uses the typename T , we need qualification with
      :: ?

      regards,
      Aman.


      --
      Posted via Mailgate.ORG Server - http://www.Mailgate.ORG

      Comment

      • Jonathan Mcdougall

        #4
        Re: function template compilation error.

        > This works fine now , thank you very much .

        Acutally, it shouldn't, I missed something :

        template <typename T>
        test<T>::A* test<T> :: function(T in)

        should be

        template <typename T>
        typename test<T>::A* test<T> :: function(T in)

        [color=blue]
        > what's the general rule on using qualification for the types ?
        > when ever a struct etc uses the typename T , we need qualification with
        > :: ?[/color]

        In your example

        template <typename T>
        class test
        {
        public :
        A* function(T);

        private :
        struct A
        {
        T somedata ;
        int i ;
        } ;
        };

        'A' is an inner class of test, so it must be qualified as 'test::A' and
        since 'test' is a template, the syntax is 'test<T>::A', where T could
        be anything else.

        Furthermore, 'A' is a dependent name of the template so it must be preceeded
        by the keywork 'typename' to denote that 'A' is a type. Note that is only
        a summary of the rules, you could get C++ Templates by Josuttis and
        Vandevoorde (very recommended) to have more informations.


        Jonathan


        Comment

        • Aman

          #5
          Re: function template compilation error.


          hmm..works fine with and without the "typename" !!

          Thanks a lot for the summary .
          regards,
          Aman.


          --
          Posted via Mailgate.ORG Server - http://www.Mailgate.ORG

          Comment

          • Nick Savoiu

            #6
            Re: function template compilation error.

            Not all compilers complain (yet) about the missing "typename".

            Nick

            "Aman" <aman@techie.co m> wrote in message
            news:576505b7fd 320273de5cc8c1c 5218a54.26421@m ygate.mailgate. org...[color=blue]
            >
            > hmm..works fine with and without the "typename" !!
            >
            > Thanks a lot for the summary .
            > regards,
            > Aman.
            >
            >
            > --
            > Posted via Mailgate.ORG Server - http://www.Mailgate.ORG[/color]


            Comment

            • Aman

              #7
              Re: function template compilation error.

              > Not all compilers complain (yet) about the missing "typename".[color=blue]
              >
              > Nick[/color]

              gotcha !
              guess it's a good idea to use it anyway .

              thanx
              Aman.


              --
              Posted via Mailgate.ORG Server - http://www.Mailgate.ORG

              Comment

              • Jonathan Mcdougall

                #8
                Re: function template compilation error.

                > > Not all compilers complain (yet) about the missing "typename".

                Which is why I missed it.
                [color=blue]
                > gotcha !
                > guess it's a good idea to use it anyway .[/color]

                Of course, since your code will eventually break one day and you
                won't have the least idea why.


                Jonathan


                Comment

                Working...