944,092 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2575
  • C RSS
Apr 17th, 2006
0

templates and maps

Expand Post »
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?
Similar Threads
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 17th, 2006
0

Re: templates and maps

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 );
}
Reputation Points: 10
Solved Threads: 1
Newbie Poster
eCharisma is offline Offline
8 posts
since Apr 2006
Apr 17th, 2006
0

Re: templates and maps

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: help with pointers in this fucntion
Next Thread in C Forum Timeline: Help! Unknown bug in Writing to file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC