Iterating a template pointer at compile time

Hi,

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
2
3
4
5
6
7
8
9
10
template <typename T>
struct Qnode 
{
	T* element;

	Qnode()
	{
		element = NULL;
	}
};


I have two questions:

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.?


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
template <typename T>
class cQueue
{
private:
	Qnode<T> QPtr;
	int size = 0;
	int front;
	int rear;

public:
	cQueue(int s)
	{
		size = s+1;
		QPtr = new Qnode<T>[size];
	
		front = rear = 0;
	}

	void enqueue(T* elem)
	{
		if ((rear+1) == front)
		{
			cout << "Errr.... Queue is full" << endl;
		}
		else
		{
			rear = (rear + 1) % size;
			QPtr + rear;
			QPtr->element = elem;
		}
	}

};


Kindly help.
Last edited on
Since at compile time the data type is unknown
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>*.
Last edited on
Hello ganado,

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.
@ubaidm, the assumptions you're making are wrong.

The information @Ganado has given you is good. As usual.
Last edited on
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!).
Well..!!

Thanks guys, i worked around my problem of a dynamic queue. And its running now...

The code is as 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
#pragma once
#include <iostream>
using namespace std;


template <typename T>
class ACQueue
{
private:
	T* QPtr;
	int size = 0;
	int front;
	int rear;

public:
	ACQueue(int s = 10)
	{
		size = s+1;
		QPtr = new T[size];
	
		front = rear = 0;
	}

	void enqueue(T elem)
	{
		cout << "Entered enqueue Rear:" << rear << " ,Front:" << front << endl;
		if ((rear+1) == front)
		{
			cout << "Errr.... Queue is full" << endl;
		}
		else
		{
			rear = (rear + 1) % size;
			cout << "Rear:" << rear << " ,Front:" << front << endl;
			QPtr[rear] = elem;
		}
	}

	void dequeue()
	{
		cout << "Entered deque Rear:" << rear << " ,Front:" << front << endl;
		if ((rear) == front)
		{
			cout << "Errr.... Queue is empty" << endl;
		}
		else
		{
			front = (front + 1) % size;
			cout << "Rear:" << rear << " ,Front:" << front << endl;
			cout << "Popped:" << QPtr[front]<< endl;
		}
	}


};

Your code looks much better.

I have 2 questions for you.

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?
Last edited on
Topic archived. No new replies allowed.