can't use typedef of nested class in template class

Discussion in 'C++' started by Frank Bergemann, Dec 12, 2007.

  1. following doesn't work (gcc-3.4.4)

    > cat test2.h

    template <class Result>
    class MyClass
    {
    private:
    class NestedClass
    {
    public:
    typedef int SubType;
    } ;
    NestedClass::SubType element;
    } ;

    > cat test2.cc

    #include "test2.h"

    > /usr/local/gcc-3.4.4/bin/g++ -c test2.cc

    In file included from test2.cc:1:
    test2.h:12: error: expected `;' before "element"


    When i move NestedClass out, it works:

    |padsol05 371> cat test2.h
    class NestedClass
    {
    public:
    typedef int SubType;
    } ;

    template <class Result>
    class MyClass
    {
    private:
    NestedClass::SubType element;
    } ;


    ???
     
    Frank Bergemann, Dec 12, 2007
    #1
    1. Advertising

  2. Frank Bergemann

    Ian Collins Guest

    Frank Bergemann wrote:
    > following doesn't work (gcc-3.4.4)
    >
    >> cat test2.h

    > template <class Result>
    > class MyClass
    > {
    > private:
    > class NestedClass
    > {
    > public:
    > typedef int SubType;
    > } ;
    > NestedClass::SubType element;


    You have to tell the compiler NestedClass::SubType is a type:

    typename NestedClass::SubType element;

    --
    Ian Collins.
     
    Ian Collins, Dec 12, 2007
    #2
    1. Advertising

  3. On Dec 12, 2:27 am, Ian Collins <> wrote:
    > Frank Bergemann wrote:
    > > following doesn't work (gcc-3.4.4)

    >
    > >> cat test2.h

    > > template <class Result>
    > > class MyClass
    > > {
    > > private:
    > > class NestedClass
    > > {
    > > public:
    > > typedef int SubType;
    > > } ;
    > > NestedClass::SubType element;

    >
    > You have to tell the compiler NestedClass::SubType is a type:
    >
    > typename NestedClass::SubType element;


    The given example works in Comeau and the FAQ only talks about
    nested classes defined in a base class (35.18). As far as I
    know, because NestedClass is not a template, there is no need
    to do this, although I may be wrong.

    --
    Jonathan Mcdougall
     
    Jonathan Mcdougall, Dec 12, 2007
    #3
  4. Frank Bergemann

    James Kanze Guest

    On Dec 12, 8:27 am, Ian Collins <> wrote:
    > Frank Bergemann wrote:
    > > following doesn't work (gcc-3.4.4)


    > >> cat test2.h

    > > template <class Result>
    > > class MyClass
    > > {
    > > private:
    > > class NestedClass
    > > {
    > > public:
    > > typedef int SubType;
    > > } ;
    > > NestedClass::SubType element;


    > You have to tell the compiler NestedClass::SubType is a type:


    > typename NestedClass::SubType element;


    You shouldn't have to. NestedClass is not a dependent type, but
    rather a locally defined type, so the compiler knows what is in
    it, even without instantiating the template.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Dec 12, 2007
    #4
  5. - thanks, Ian!
    it worked this way.

    rgds!

    Frank
     
    Frank Bergemann, Dec 12, 2007
    #5
  6. Frank Bergemann

    Ian Collins Guest

    James Kanze wrote:
    > On Dec 12, 8:27 am, Ian Collins <> wrote:
    >> Frank Bergemann wrote:
    >>> following doesn't work (gcc-3.4.4)

    >
    >>>> cat test2.h
    >>> template <class Result>
    >>> class MyClass
    >>> {
    >>> private:
    >>> class NestedClass
    >>> {
    >>> public:
    >>> typedef int SubType;
    >>> } ;
    >>> NestedClass::SubType element;

    >
    >> You have to tell the compiler NestedClass::SubType is a type:

    >
    >> typename NestedClass::SubType element;

    >
    > You shouldn't have to. NestedClass is not a dependent type, but
    > rather a locally defined type, so the compiler knows what is in
    > it, even without instantiating the template.
    >

    I know, but gcc thinks otherwise!

    --
    Ian Collins.
     
    Ian Collins, Dec 13, 2007
    #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.

Share This Page