Results 1 to 3 of 3

Thread: templates used on map container

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    templates used on map container

    I've got the following code:
    Code:
    template <typename T> int Histogram<T>::get_occurances(const T& obj)
    {
             map<T,int>::iterator find_occur = frequency.find(obj);
             
             if ( find_occur == this->frequency.end() )
             {
                return 0;
             }
             
             return ( find_occur->second );
    }
    The following line always gives me an error saying ';' expected before find_occur.

    map<T,int>::iterator find_occur = frequency.find(obj);

    If I change the first argument of the map to something other than T, then it works fine, but I need the template type. Is there something I'm doing wrong?

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: templates used on map container

    Assuming you have a recent compiler the code should be:
    Code:
    typename map<T,int>::iterator find_occur = frequency.find(obj);
    The typename indicates to the compiler that iterator is a type and not another kind of class member.

  3. #3

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: templates used on map container

    Quote Originally Posted by twanvl
    Assuming you have a recent compiler the code should be:
    Code:
    typename map<T,int>::iterator find_occur = frequency.find(obj);
    The typename indicates to the compiler that iterator is a type and not another kind of class member.
    Thanks twanvl,

    I would have never figured this out. I've always been confused as to why you must use the typename keyword like that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width