Template object within template member function

I've been having a problem with my gcc compiled program. The program itself is a text-based MUD server that I've been working on for a while. The problem comes at two points in the program where I use a template class Menu to create menus that the user can navigate. The linked list class has worked flawlessly on nontemplated classes throughout my program, except in this one instance. When I try to declare an iterator for a (custom) templated linked list class, the compile dies with the error:

'q' was not declared in this scope

Here's the partial linked list class definition

1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename T>
class linklist
{
	public:
		struct node
		{
			T data;
			node *link;
		}*p;

		...

};


And here's the troublesome section of code.

1
2
3
4
5
6
7
8
9
10
11
12
template<class T>
Menu<T>* Menu<T>::select(string sel){
	Menu<T>* ret=NULL;
	sel=strupr(sel);
	linklist<Menu<T>*>::node* q;
	for(q=contents.p;q!=NULL;q=q->link)
		if(strupr(q->data->ident()).find(sel)!=string::npos){
			if(ret) return NULL;
			ret=q->data;
		}
	return ret;
}


Can anyone spot the problem?
Yes, you need to use the typename keyword on line 5:

5 typename linklist<Menu<T>*>::node* q;

Hope this helps.
Last edited on
Thanks a million, this has been giving me trouble for days. I'm still a bit new to template programming and I'm pretty confused as to when I need to include specific keywords for my templates. How did you know to use the typename keyword?
Because you are trying to use a dependent type name.

Here is some good reading:
http://pages.cs.wisc.edu/~driscoll/typename.html
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18

It caught me off-guard the first time too. These days, I tend to do the same thing as the STL and typedef this kind of stuff:
1
2
3
4
5
6
7
template <typename DataType>
struct List:
  std::list <DataType>
  {
  typedef typename std::list <DataType> ::size_type size_type;

  ...

After this example, I can create Lists and use the size_type directly:
1
2
3
List <int> xs;
for (List <int> ::size_type n = 0; n < xs.size(); n++)
  ...

Glad to be of help.
Topic archived. No new replies allowed.