I am trying to print the contents of a queue as an array. I have the code working and compiling. The problem is when I call the function more than once the print function does not call and the array is not printed again. I need to print the array more than once and it does not print.
The code for the Print function is :
template <class Type>
void queueType<Type>::debugArray()
{
for(queueFront; queueFront<count; queueFront++){
cout << "[" << queueFront<< "] ," << list[queueFront] << " ";
}
} //end debugQueue
The main.cpp code is:
#include <iostream>
#include "queueAsArray.h"
using namespace std;
int main()
{
queueType<int> q1;
queueType<int> q2;
queueType<int> q3;
int x = 5;
for(int i= 0; i<10; i++) {
q1.addQueue(i);
}
cout << "q1 after being filled up with 10 items" << endl;
q1.printQueueInfo();
cout << "Queue contents from front to rear\n\n" << endl;
q1.debugArray();
q1.deleteQueue();
q1.deleteQueue();
q1.deleteQueue();
for(int i= 0; i<=20; i){
i+=5;
q1.addQueue(i);
}
q1.debugArray();
return 0;
}
Is there a reason why the function call will not print again? If you need the entire class and implementation file I can supply it. The weird thing is if I create a second instatiation of the class q2, then build an array for q2, the debugQueue function prints that queue. I then call the overloaded assignment operator and do q2=q1, then call debugQueue Again and it prints the contents of the queue. So I am confused as why it will print the second queue twice, but not the first queue.
Any thoughts?
It looks like your problem is that you’re mutating the
queueTypeinstance as a part of the printing.Here you are taking the member
queueFrontand mutating it forward until you hit the end of the queue. This is mutating the fieldqueueFrontinstead of a local which points to the same location. Try using a local and it will fix your problemNote: I used
autofor the type because I didn’t know the type ofqueueFront. I assume it’slist<int>::iteratorbut wasn’t sure. You can replaceautowith the correct type or leave it as is and let the compiler infer the type ofcurrent