Declaring an STL list iterator in a class

Dec 3, 2008 at 4:14pm
I built a simple Queue class using the Standard Template Library "list" as the container for the Queue.

My problem arises when I try to provide a member function that prints the contents of the Queue. I'm simply trying to create a list iterator object however it fails to compile and I'm not sure why. Below is my code.

Header File for the Queue class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef QUEUE_H
#define QUEUE_H
#include <list>
using namespace std;

template <class Type>
class Queue
{
	public:
		// Constructor
		Queue();
		Queue(const Queue& source);
		
		// Destructor
		~Queue();
		
		// Modification Member Functions
		void insert(const Type& entry);
		Type get_front();
		void operator =(const Queue& source);
		
		// Constant Member Functions
		size_t size() const;
		bool is_empty() const;
		void printHorz() const;
	
	private:
		list<Type> data;
};

#include "queue.cpp"

#endif 


Implementation file for the Queue class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include "queue.h"

/*******************************************************
		             CONSTRUCTORS
*******************************************************/
template <class Type>
Queue<Type>::Queue()
{
	// nada
}

template <class Type>
Queue<Type>::Queue(const Queue<Type>& source)
{
	data = source.data;
}
/*******************************************************
		             DESTRUCTOR
*******************************************************/
template <class Type>
Queue<Type>::~Queue()
{
	data.clear();
}

/*******************************************************
		     MODIFICATION MEMBER FUNCTIONS
*******************************************************/
template <class Type>
void Queue<Type>::insert(const Type& entry)
{
	data.push_back(entry);
}

template <class Type>
Type Queue<Type>::get_front()
{
	Type answer;
	
	if(!is_empty())
	{
		answer = data.front();
		data.pop_front();
		return answer;
	}
}

/*******************************************************
		     CONSTANT MEMBER FUNCTIONS
*******************************************************/
template <class Type>
size_t Queue<Type>::size() const
{
	return data.size();
}

template <class Type>
bool Queue<Type>::is_empty() const
{
	return data.empty();
}

template <class Type>
void Queue<Type>::printHorz() const
{
	list<Type>::iterator it;
	
	for (it = data.begin(); it != data.end(); it++)
		cout<<" "<<*it;
}


The compiler does not like the line list<Type>::iterator it;. It gives me the following error:

In member function `void Queue<Type>::printHorz() const [with Type = int]':
[line 67] dependent-name ` std::list<Type,std::allocator<_CharT> >::iterator' is parsed as a non-type, but instantiation yields a type. Say `typename std::list<Type,std::allocator<_CharT> >::iterator' if a type is meant


I've included a sample main program below.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <conio.h>
#include "queue.h"
using namespace std;

int main()
{
	// Declare Variables
		Queue<int> a, b;
		int i;

	//Fill in Queue a
		a.insert(1);
		a.insert(2);
		a.insert(3);
		a.insert(4);
		a.insert(5);
		a.insert(6);
		a.insert(7);
		a.insert(8);
		a.insert(9);
		a.insert(10);

	//Fill in Queue b
		b.insert(1);
		b.insert(2);
		b.insert(3);
		b.insert(4);
		b.insert(5);
	
	// Copy Queue a
		Queue<int> c(a);

	//Print the Queue sizes
		cout<<"Queue A's size is: "<<a.size()<<endl;
		cout<<"Queue B's size is: "<<b.size()<<endl;
		cout<<"Queue C's size is: "<<c.size()<<endl;
		
	//Print the Queues
		cout<<"\n\n\nQueue A:";
		a.printHorz();
		cout<<"\nQueue A's size is: "<<a.size()<<endl;

		cout<<"\n\nQueue B:";
		b.printHorz();
		cout<<"\nQueue B's size is: "<<b.size()<<endl;
			
		cout<<"\n\nQueue C:";
		c.printHorz();
		cout<<"\nQueue C's size is: "<<c.size()<<endl;

	
	// Use the get character function to pause the program
		getch();

	return 0;
}
Dec 3, 2008 at 4:59pm
Did you try adding typename, like the compiler told you?
Dec 3, 2008 at 5:06pm
As in typename list<Type>::iterator it?

Yes, I did.

Didn't work. I tried using the exact phrase it told me to try.

I'm curious behind the meaning of the error as well. Does the declaration of this iterator require strict type or is it possible to use dynamic type for template use? My assumption is you can use a dynamic type but don't know how to write it correctly.
Last edited on Dec 3, 2008 at 5:21pm
Dec 3, 2008 at 5:42pm
You need const_iterator in that function.
Dec 3, 2008 at 5:54pm
I was able to compile it after I changed a couple of things.

Instead of using list<Type>::iterator it;, I used _List_iterator<Type> it;.

I also removed the const declaration from the functions. (i.e. void printHorz(); instead of void printHorz() const;. I did this also in the implementation file.

The reason I removed the const declaration was due to another compile error I received. It stated it didn't like that I was doing this statement it = data.begin().

Does anyone know why the list iterator can't point to something within a const function?
Dec 3, 2008 at 5:58pm
Thanks jsmith.

I changed the line to _List_const_iterator<Type> it; and it allowed me to declare the functions as const.
Topic archived. No new replies allowed.