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:
// question 7, mastery check 12
// create a queue (FIFO), generic object
// Queue class - generic
#include <iostream>
#include <new>
usingnamespace std;
constint 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;
elseif(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
]
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
// 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.