DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   templates and maps (http://www.daniweb.com/forums/thread43514.html)

server_crash Apr 17th, 2006 5:10 pm
templates and maps
 
I've got the following 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?

eCharisma Apr 17th, 2006 7:35 pm
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 );
}

server_crash Apr 17th, 2006 7:44 pm
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.


All times are GMT -4. The time now is 2:49 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC