user2626195
user2626195

Reputation: 147

Howto use a c++ template typedef in a template class

I'd like to typedef a template type and use this type in a second template class.

First I defined the typedef with a helper struct

template<class T>
struct MyList {
   typedef std::map<int, T> Type;  
};

and then used it in the second template:

 template <class T>
    class MySecondClass {
    public:
       MySecondClass(MyList<T>& list) : list_(list) {}
    private:
       MyList<T>::Type list_;   
   };

Unfortunately, the use of MyListT& list; doesn't work and creates an error.

Upvotes: 2

Views: 224

Answers (2)

YK1
YK1

Reputation: 7602

I think this is what you want:

template <typename T>
 class MySecondClass {
    public:
       MySecondClass(typename MyList<T>::Type& list) : list_(list) {}
    private:
       typename MyList<T>::Type& list_;   
  };

Upvotes: 2

Ben Jackson
Ben Jackson

Reputation: 93700

MyList<T>::Type is not the same type as MyList<T> and you are mixing them in MySecondClass. Also you may need a typename prefix on that last declaration.

Upvotes: 1

Related Questions