• Advertisement
Sign in to follow this  

templated function not working

This topic is 3351 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

template <class KeyT , class ValT>
inline QMap<KeyT, ValT>::const_iterator incrementIterator(const QMap<KeyT, ValT>::const_iterator& it, const QMap<KeyT, ValT>::const_iterator& endItr, int i){
	int itr = 0;
	while(it != endItr || itr++ <= i)++it;
	return it;
}
Why the above code doesn't compile and fires compilation error.
expected initializer before ‘incrementIterator’

Share this post


Link to post
Share on other sites
Advertisement
a) Could you please post more context?
b) Could you please reformat your code, say, with 1 expression per line?
c) Could you please post the complete error-log?

Share this post


Link to post
Share on other sites
Whenever you have a nested type within a template class like this:


template <class KeyT , class ValT>
inline QMap<KeyT, ValT>::const_iterator ...


the typename keyword is mandatory in front of QMap to tell that what follows is a type (and not for example a static member - the compiler shouldn't need to wait until QMap<KeyT, ValT> is instantiated to find that out), except with VC++.

Share this post


Link to post
Share on other sites
You probably want:
inline typename QMap<KeyT, ValT>::const_iterator incrementIterator(const typename QMap<KeyT, ValT>::const_iterator& it, const typename QMap<KeyT, ValT>::const_iterator& endItr, int i){

Share this post


Link to post
Share on other sites
My bet would be that the compiler misses a "typename" keyword in front of the "QMap<KeyT, ValT>::const_iterator" and is unsure what to make of it.

Share this post


Link to post
Share on other sites
I did also put typename in argument list and return type however I wrongly put it before const which did not worked now when I put it after const it worked.Thanks

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement