Thread: typedef and typename

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    typedef and typename

    I'm trying to return an iterator to a map from it's wrapper class. Finally got it to compile but wanted to ask why this didn't work.

    Code:
    template <typename key, typename value>
    class MgrBase
    {
        typedef typename std::map< key, value> MgrMap;
        typedef typename MgrMap::iterator MapIter;
        public:
             MgrBase();
             virtual ~MgrBase();
            
             MapIter Get(key ID);     
             size_t getMemSize();
    };
    
    template <typename key,typename value> 
    MgrBase<key,value>::MapIter MgrBase<key,value>::Get(key ID)
    {
        MgrMap::iterator map_iter(m_Objects.find(ID));
               
        return map_iter;
    }
    To get this to compile I had to add typename to the front of the function prototype and definition.

    Code:
    template<typename key,typename value>
    class MgrBase
    {
    ...
          typename MapIter Get(key ID);     
    ...
    };
    
    template <typename key,typename value> 
    typename MgrBase<key,value>::MapIter MgrBase<key,value>::Get(key ID)
    {
        MgrMap::iterator map_iter(m_Objects.find(ID));
               
        return map_iter;
    }
    Why wasn't this enough?
    Code:
    typedef typename MgrMap::iterator MapIter;
    Seems to me the typename is in the typedef so why did I have to add typename to the declaration and definition?
    Last edited by VirtualAce; 07-12-2008 at 04:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use of a class in place of main( )?
    By Sebastiani in forum C++ Programming
    Replies: 15
    Last Post: 12-10-2008, 01:06 PM
  2. Need Partners (D&D fan preferably)
    By C_ntua in forum Game Programming
    Replies: 44
    Last Post: 11-22-2008, 09:21 AM
  3. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  4. typedef VS typename
    By tilex in forum C++ Programming
    Replies: 9
    Last Post: 07-25-2004, 03:26 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM