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.
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
#include <iostream>
#include <conio.h>
#include "queue.h"
usingnamespace 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;
}
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.
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?