how do I access a member class of a class template from a template child class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • presencia
    New Member
    • Sep 2009
    • 14

    #1

    how do I access a member class of a class template from a template child class

    Hi all,
    I got the following problem. I have two class templates, one inheriting from the other.

    Code:
    template<typename T>
    struct M{
    	class A{};
    };
    
    template<typename T>
    class B:public M<T>{
    	A a;
    };
    I know that this code is not correct. I've read about the compiler's two-phase lookup of members in template classes. So I assume that giving the fully qualified name would work. But it doesn't: If I replace the
    Code:
    A a;
    with
    Code:
    M<T>::A a;
    the compiler (gcc 4.4.3) complains about this line with
    Code:
    error: ISO C++ forbids declaration of ‘A’ with no type
    error: expected ‘;’ before ‘a’
    My questions are:
    1. Is there any way to use the A class in my B class?
    2. I have read that for non-type members of the class template M I can use the "using" declaration inside the child class to be able to use it like its own member. Is there anything I like this for type-members like class A?
    I would gratefully appreciate any help.
    ~<><~~~~~~~ presencia
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The problem is that because M is a template class the compiler can not tell what type of thing M<T>::A is and assumes it is a symbol. You need to tell the compiler it is a type name like this

    Code:
    template<typename T>
    class B: public M<T>{
        typename M<T>::A a;
    };

    Comment

    • presencia
      New Member
      • Sep 2009
      • 14

      #3
      Thanks Banfa,
      I don't know why I didn't try just this before. It works, thank you. This thread is solved.
      ~<><~~~~~~~ presencia

      Comment

      Working...