I was doing some sort of infix-to-postfix conversion.
My program works fine but at the end it gives some sort of error!
I guess the error is on the void printQueueContents(Queue typeInfo)
method of following class
template <class T>
class Queue{
private:
int front, rear, max;
T *q;
public:
Queue(int size=0){
front = rear = 0;
max = size;
q = new T[size];
}
~Queue(){
delete []q;
}
int enqueue(T);
T dequeue();
T getValueFromIndex(int index){
return q[index];
}
void printQueueContents(Queue typeInfo){
for(int i=rear; i<front;i++){
if(typeInfo.getValueFromIndex(i)) {
cout<<q[i]<<" ";
} else {
cout<<static_cast<char>(q[i])<<" ";
}
}
cout<<"\n";
}
};
which is called as:
q1.printQueueContents(q2);
And declared as:
Queue<int> q1(200);
Queue<int> q2(200); // Holds data type information for q1
I cannot figure out why is this happening? Any suggestions?
EDIT: Thanks to peoples with answers. I finally managed to solve it by changing the method to:
void printQueueContents(Queue& typeInfo)
so that while calling this function the default copy constructor does not make a copy and destructor does not accidently deletes the same memory location twice. Here's a link that has a intuitive explaination. http://www.drdobbs.com/c-made-easier-the-rule-of-three/184401400