Skip to main content
gamedev.net
🔒 Locked

C++ template problem

Started by Mawww 6 replies 500+ views
Original Post
Mawww
Mawww
g++ have trouble with that : std::vector< TemplatedStructure<T> >::const_iterator iter; expected ; before iter... I'm new to templates but I have not found any thing about this... whats the problem ?
Tchou kanaky ! tchou !
joanusdmentia
joanusdmentia
Looks fine on it's own. What version of g++ are you using? Also post a couple of the lines before that line as well as the declaration of TemplatedStructure.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
Basiror
Basiror
did you set the end ; in your templatedstructure?

http://www.8ung.at/basiror/theironcross.html
Mawww
Mawww
template<typename T> struct mwCacheItem{	T* item;	std::string name;	mwCacheItem (T* theItem, std::string theName) 		: item(theItem), name(theName)	{}	~mwCacheItem () { if (item) delete item; }}; template<typename T> class mwCacheTemplate{private:	std::vector< mwCacheItem<T> > itemList;public:	T* 		query		(std::string name)	{		std::vector< mwCacheItem<T> >::const_iterator iter;		for (iter 				 = itemList.begin(); iter != itemList.end(); iter++)		{			if ((*iter).name == name)				return (*iter).item;		}...


that's the relevent part i think
Tchou kanaky ! tchou !
Fruny
Fruny
Assuming T is a yet-unspecified template parameter, std::vector< TemplatedStructure<T> >::const_iterator is a dependent name (it is a member of a class template which hasn't been fully specialized yet). C++ compilers assumes that a dependent name refers to a static variable unless you use the keyword typename.

typename std::vector< TemplatedStructure<T> >::const_iterator iter;
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
jflanglois
jflanglois
Well? Don't you want to specify a valid template parameter?

Id est:
std::vector< TemplatedStructure<T> >::const_iterator iter;
Turns into:
std::vector< TemplatedStructure<int> >::const_iterator iter;

T is not a type. (edit: as fruny touches upon, at least not typically)


jfl.


[edit] And beaten... Oh well. Besides, fruny's reply is better.
Mawww
Mawww
that was it ! thanks for your replies.
Tchou kanaky ! tchou !
Fruny
Fruny
The reason it is so is because you could very well do:

template<class T> class foo{   typedef T f;};template<> class foo<int>{   static int f;};


If I write foo<double>::f, the compiler can go and check what f is in foo<double> - a type. But if I write:
template<class T> class bar{   foo<T>::bar b;};


the compiler cannot check if the syntax is correct without deciding if foo<T>::bar should be a static variable (in which case the syntax is incorrect) or a nested type (in which case it is correct). The default choice is to treat it as a static variable. The addition of typename causes the compiler to treat it as a type.

template<class T> class bar{   typename foo<T>::bar b;};



Templates are syntax-checked where they are defined, but checks that depend on actually knowing the template pararameters (like, is a<b legal when a and b are of type quux) are obviously deferred until the point at which the template is used (which means that your program may compile even though some functions have errors if you don't actually call those functions).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.