typedef'd return type of a template class member function: take 2

Discussion in 'C++' started by Pete Becker, Nov 18, 2004.

  1. Pete Becker

    Pete Becker Guest

    Dave wrote:
    >
    > The following code won't compile for me, it does not
    > recognize my typedef'd type as a type:


    In general, you should include the exact error message that you got.

    >
    > template <class T> A<T>::TD A<T>::b () {


    Probably need:

    template <class T> typename A<T>::TD A<T>::b () {

    A<T>::TD is assumed to name a data object unless you tell the compiler
    that it's the name of a type.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Nov 18, 2004
    #1
    1. Advertising

  2. Pete Becker

    Dave Guest

    The following code won't compile for me, it does not
    recognize my typedef'd type as a type:

    template <class T> class A {
    public:
    typedef int TD;
    private:
    TD b ();

    };

    template <class T> A<T>::TD A<T>::b () {

    // Stuff.

    }

    This would work fine if it was not a template class, it's something I
    use regularly to keep my class's types clear and consistent without
    polluting their containing namespace. Can anyone tell me how I can
    accomplish this with a template?

    Thanks!
    Dave Corby

    P.S.
    Sorry about the lack of indentation, I'm at work and can't install a
    real NNTP client so I'm posting this through Google.
     
    Dave, Nov 19, 2004
    #2
    1. Advertising

  3. "Dave" <> wrote in message
    news:...
    > The following code won't compile for me, it does not
    > recognize my typedef'd type as a type:
    >
    > template <class T> class A {
    > public:
    > typedef int TD;
    > private:
    > TD b ();
    >
    > };
    >
    > template <class T> A<T>::TD A<T>::b () {
    >
    > // Stuff.
    >
    > }


    You need to tell the compiler that TD is the name of a type. Try this:

    template <class T> typename A<T>::TD A<T>::b ()
    {
    // Stuff.

    }

    (Note 'typename'...)

    Ali
     
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Nov 19, 2004
    #3
  4. Pete Becker

    Dave Guest

    I tried out what you have both suggested, and now I get the error:

    error: no `typename A<T>::TD A<T>::b()' member function declared in
    class `A<T>'

    Now it looks like all I have to do is get the declaration to match the
    definition, but I can't imagine how I can change my declaration and
    still have it mean the same thing.

    Thanks for all your help so far, and sorry about my posting skills, I
    am working on them though :)
     
    Dave, Nov 19, 2004
    #4
  5. Pete Becker

    Dave Guest

    Oh, so sorry, I found my problem. Your solution fixed it, I just
    mistyped a character in the function name and posted again too hastily.
    Thank you both again very much!
     
    Dave, Nov 19, 2004
    #5
  6. "Dave" <> wrote in message
    news:...
    > I tried out what you have both suggested, and now I get the error:
    >
    > error: no `typename A<T>::TD A<T>::b()' member function declared in
    > class `A<T>'


    This is the complete code that works with g++ both 3.4.2 and 2.95.3:

    template <class T> class A
    {
    public:
    typedef int TD;
    public:
    TD b ();
    };

    template <class T> typename A<T>::TD A<T>::b ()
    {
    // Stuff.
    }

    int main()
    {}

    Maybe your compiler is too old?

    > Now it looks like all I have to do is get the declaration to match the
    > definition,


    This is not needed and wouldn't work. You use the 'typename' keyword
    whenever a name may change meaning depending on a template parameter. For
    example, it is possible that a specialization of the template can introduce
    the same name as an object.

    Ali
     
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Nov 19, 2004
    #6
    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. Replies:
    3
    Views:
    469
    Victor Bazarov
    Nov 19, 2004
  2. Replies:
    2
    Views:
    440
    Aleksey Loginov
    Nov 7, 2005
  3. oor
    Replies:
    0
    Views:
    1,358
  4. Replies:
    1
    Views:
    610
  5. K. Frank
    Replies:
    8
    Views:
    653
    K. Frank
    Mar 22, 2012
Loading...

Share This Page