I am trying to implement a circular array based Queue, i want my queue to be able to work on any data type, hence the data structure i used is a template.
1.since my queue is array based, how will i create queue size, as that is something which i can only know at run time. I am having a problem initializing the struct variable in my class template.?
2. Since in circular queue i need to iterate from the front and the back to enqueue and deque, how will i do it..? Since at compile time the data type is unknown and any incrementing operation on my templatized data type throws me an error.?
C++ is a strongly (edit: I mean statically) typed language, so it's not possible that a type is unknown at compile time (unless you're referring to polymorphic object, in which case at least some information of the base class will at least be known).
(1.)
Your "QPtr" isn't a pointer, you have it defined as a Qnode<T>. If you want it to be a pointer, then do Qnode<T>* QPtr;
(2.)
QPtr + rear;
Your line 28 doesn't do anything. But I'm not sure what you're trying to do here. rear and front are integers, so it doesn't make sense to add them to a Qnode<T> or Qnode<T>*.
1. In my constructor, when I initialize QPtr = new Qnode<T>[size ]
It asks me to define size, whereas I can only know the size at run time.
2. I'm just Incrementing the pointer via QPtr + rear
Since type of QPtr is not known at compile time as it is a template, I get an compile time error on not overloaded ++ operator or + operator.
so it's not possible that a type is unknown at compile time... nitpick a little on this statement:
a pointer is a pointer. You can make one without a type, but its kind of C-ish (void* is a placeholder pointer of unknown type). Not saying to DO this (it isnt right here, not AT ALL) but pointers are kind of 'soft' in the 'strongly typed' world of c++ .. you can cast any pointer into any other type that exists. Whether you should or not (usually, you should not!).
1. Why add 1 to 's' in line 18? You are just making a queue that has 1 more element than was requested. So, intsead of a a 10-element queue, you have an 11-element queue.
2. What happens if front == 0 and rear == (size - 1) and you try to enqueue another value?