function template compilation error.

Discussion in 'C++' started by Aman, Oct 1, 2003.

  1. Aman

    Aman Guest

    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
    Aman, Oct 1, 2003
    #1
    1. Advertising

  2. > 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 ;
    > } ;
    >
    > };


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


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


    Should be

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


    Jonathan
    Jonathan Mcdougall, Oct 1, 2003
    #2
    1. Advertising

  3. Aman

    Aman Guest

    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
    Aman, Oct 1, 2003
    #3
  4. > 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)


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


    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
    Jonathan Mcdougall, Oct 1, 2003
    #4
  5. Aman

    Aman Guest

    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
    Aman, Oct 1, 2003
    #5
  6. Aman

    Nick Savoiu Guest

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

    Nick

    "Aman" <> wrote in message
    news:...
    >
    > 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
    Nick Savoiu, Oct 1, 2003
    #6
  7. Aman

    Aman Guest

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


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

    thanx
    Aman.


    --
    Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
    Aman, Oct 1, 2003
    #7
  8. > > Not all compilers complain (yet) about the missing "typename".

    Which is why I missed it.

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


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


    Jonathan
    Jonathan Mcdougall, Oct 2, 2003
    #8
    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. , India
    Replies:
    2
    Views:
    312
  2. C__chp
    Replies:
    4
    Views:
    493
    Puppet_Sock
    Feb 15, 2008
  3. sunil
    Replies:
    1
    Views:
    366
    Ian Collins
    Aug 15, 2008
  4. , India
    Replies:
    0
    Views:
    266
    , India
    Apr 13, 2009
  5. , India
    Replies:
    2
    Views:
    280
    mingze zhang
    Aug 30, 2010
Loading...

Share This Page