Dynamic Initlization of objects in a Queue

Hello,
I'm currently mdefying a program that creates an object of type Sample. It then uses the class Queue to put it into a Queue. Now I want to make it so whenever you add an object to the queue it uses dynamic allocation. I havent added try and catch, but that'll be in the final program once I get this working. Here is the program:
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// question 7, mastery check 12
// create a queue (FIFO), generic object
// Queue class - generic
#include <iostream>
#include <new>
using namespace std;

const int maxQsize = 100;
int count=1;

class Sample{												
	int id;
public:
	// Constructors
	Sample(){ id = count; count++; }

	// Accessor
	void show(){ cout << id << endl; }
	int getid(){ return id; }
	void putid(int x){ id = x; }
};

template <class QType> class Queue{
	QType *q[maxQsize];
	int size; // max num of elements the q can store
	int putloc, getloc; // put and get indices
public:

	// Construct a queue of a specific length
	Queue(int len){
		// Queue must be less than max and positive
		if(len > maxQsize) len = maxQsize;
		else if(len <=0) len = 1;

		size = len;
		putloc = getloc = 0;
	}

	// Put an element of type QType into the Queue
	void put(QType t){
		if(putloc == size) {
			cout << " -- Queue is full.\n";
			return;
		}

		QType *p;

		p = &t;

		p = new QType;
		
		putloc++;
		q[putloc] = *p;
	}

	// get an element of type QType from the q
	QType get(){
		if(getloc == putloc){
			cout << " -- Queue is empty.\n";
			return 0;
		}

		delete p;

		getloc++;
		return q[getloc];
	}
};

// Demo the generic queue class
int main()
{
	Queue<Sample> q(3); // 3 is len
	Sample ob1, ob2, ob3, rec[3];

	q.put(ob1);
	q.put(ob2);
	q.put(ob3);
	
	rec[0].putid(10);
	rec[1].putid(20);
	rec[2].putid(30);

	cout << "Reciving the objecs in the queue (current values): ";
	for(int i=0; i<3; i++){
		cout << rec[i].getid() << ' ';
	}
	cout << endl;

	for(int i=0; i<3; i++){
		rec[i] = q.get();
	}

	cout << "Actual values recived from queue: ";
	for(int i=0; i<3; i++){
		cout << rec[i].getid() << ' ';
	}

	return 0;
}


And here are the errors I'm getting:

dynsampq.cpp
dynsampq.cpp(53) : error C2440: '=' : cannot convert from 'Sample' to 'Sample *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
dynsampq.cpp(40) : while compiling class template member function 'void Queue<QType>::put(QType)'
with
[
QType=Sample
]
dynsampq.cpp(73) : see reference to class template instantiation 'Queue<QType>' being compiled
with
[
QType=Sample
]

Thanks,

enduser000
Just to make sure you are aware. There is an STL container that does this
q[putloc] = *p; should be q[putloc] = p;
I'm going to head onto STL after this, but by the looks of it it'll solve alot of my problems, so that'll be cool. I tried q[putloc] = p; but it dosen't seem to like that ethier. Here's what I'm getting now:

dynsampq.cpp
dynsampq.cpp(60) : error C2664: 'Sample::Sample(const Sample &)' : cannot convert parameter 1 from 'int' to 'const Sample &'
Reason: cannot convert from 'int' to 'const Sample'
No constructor could take the source type, or constructor overload resolution was ambiguous
dynsampq.cpp(57) : while compiling class template member function 'Sample Queue<QType>::get(void)'
with
[
QType=Sample
]
dynsampq.cpp(73) : see reference to class template instantiation 'Queue<QType>' being compiled
with
[
QType=Sample
]
dynsampq.cpp(63) : error C2065: 'p' : undeclared identifier
dynsampq.cpp(63) : error C2541: 'delete' : cannot delete objects that are not pointers
dynsampq.cpp(66) : error C2664: 'Sample::Sample(const Sample &)' : cannot convert parameter 1 from 'Sample *' to 'const Sample &'
Reason: cannot convert from 'Sample *' to 'const Sample'
No constructor could take the source type, or constructor overload resolution was ambiguous

...?
1
2
3
4
5
6
7
8
9
10
11
12
	// get an element of type QType from the q
	QType get(){
		if(getloc == putloc){
			cout << " -- Queue is empty.\n";
			return 0;
		}

		delete p;

		getloc++;
		return q[getloc];
	}


You can't return 0 because your returning an object. You should return a pointer instead. Also, you haven't defined p.
Last edited on
Topic archived. No new replies allowed.