std::set within std::map problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • peal
    New Member
    • Dec 2008
    • 2

    std::set within std::map problem

    Hi,
    i'm trying to make a calendar but get stuck at the following compiler error.

    calendar.cpp: In member function `bool lab2::Calendar< T>::add_event(s td::string, int, int, int)':
    calendar.cpp:45 : error: expected `;' before "it"
    Here follows the snippet of code which i think is relevant to the problem. (The code in full can be found here Nopaste - calendar.cpp and Nopaste - calendar.h)

    Code:
    template <typename T>
    bool Calendar<T>::add_event( std::string event, int y, int m, int d)
    {
    	if( (y < 1858) || (m < 1) || (d < 1) || (y > 2558) 
    			|| (m > m_today.months_per_year())
    			|| (d > m_today.days_this_month(m))	)
    	{
    		return false;
    	}
    
    	T t(y,m,d);
    	map<T, set<string> >::iterator it;
    	it = m_events.find(t);
    	pair< set<string>::iterator,bool > res = it.insert(event);
    
    	return res.second;
    }
    I guess i'm using some bad syntax for the set of strings within the map, or?
    Help would be much appreciated.

    BR
    peal
  • vmpstr
    New Member
    • Nov 2008
    • 63

    #2
    You have to add keyword typename before your definition of it.

    i.e.

    Code:
    typename map<T, set<string> >::iterator it;
    Why, you ask? I only vaguely understand that this helps the compiler understand that map<T, set<string> >::iterator is a type, not something else. Shouldn't it know it by itself? Well, no, because you put in a template parameter in there, which can be anything and it can't be initialized until later.

    Is that explanation not clear? Probably not, but that's because I don't really get it myself. :D

    Comment

    • peal
      New Member
      • Dec 2008
      • 2

      #3
      Wow, that helped.
      Thanks a lot vmpstr!

      I would never had guessed that an extra "typename" would have solved this. Thought that since i put "template <typename T>" in the signature the compiler would be aware of everything concerning "T"'s. But i guess that the "typename map<T, set<string> >::iterator" type is kind of complex.. :)

      Thanks for the help and explanation!

      Comment

      Working...