templates and maps

Reply

Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

templates and maps

 
0
  #1
Apr 17th, 2006
I've got the following code:
  1. template <typename T> int Histogram<T>::get_occurances(const T& obj)
  2. {
  3. map<T,int>::iterator find_occur = frequency.find(obj);
  4.  
  5. if ( find_occur == this->frequency.end() )
  6. {
  7. return 0;
  8. }
  9.  
  10. return ( find_occur->second );
  11. }

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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 8
Reputation: eCharisma is an unknown quantity at this point 
Solved Threads: 1
eCharisma eCharisma is offline Offline
Newbie Poster

Re: templates and maps

 
0
  #2
Apr 17th, 2006
A type that depends on a template parameter needs to be qualified with the typename keyword.
template <typename T> int Histogram<T>::get_occurances(const T& obj)
{
         typename map<T,int>::iterator find_occur = frequency.find(obj);
         
         if ( find_occur == this->frequency.end() )
         {
            return 0;
         }
         
         return ( find_occur->second );
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: templates and maps

 
0
  #3
Apr 17th, 2006
Ahhh, thank you. I was actually looking into using a typename, but in a different context. I've never really understood why you have to qualify it with that.


Thanks man.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC