// workqueue.cpp -- Tests QueueTp template class
// compile with queuetp.cpp
#include <iostream>
#include "queuetp.h"
const int SIZE = 5;
int main() {
using std::cin;
using std::cout;
using std::endl;
QueueTp<Worker *> lolas(SIZE);
Worker holder[SIZE];
int ct;
cout << "Enter data for " << SIZE << " workers.\n";
for (ct = 0; ct < SIZE; ct++) {
holder[ct].Set();
cin.get();
}
for (int i = 0; i < ct; i++)
lolas.enqueue(&holder[i]);
cout << "\nYou have" << lolas.queuecount() << "workers. \n";
for (int i = 0; i < ct; i++) {
holder[i].Show();
lolas.dequeue(&holder[i]);
}
cout << "You now have" << lolas.queuecount() << "workers. \n";
cout << "Bye.\n";
return 0;
}
I'm getting the following error message from g++: "workqueue.cpp:30: error: no matching function for call to ‘QueueTp<Worker*>::dequeue(Worker*)’
queuetp.h:41: note: candidates are: bool QueueTp<Item>::dequeue(Item&) [with Item = Worker*]"
So it finds the correct function but then says it doesn't match! This apparently is connected with use or nonuse of const. The enqueue() method, which has the same signature but with a const argument, works fine. When I remove "const", it gives the same error that dequeue() does. Here are the two functions (directly from Prata):
// Add item to queue
template <typename Item>
bool QueueTp<Item>::enqueue(const Item & item) {
if ( isfull() )
return false;
Node * add = new Node; // create node
if (add == NULL)
return false; // quit if none available
add->item = item; // set node pointers
add->next = NULL;
items++;
if (front == NULL) // if queue is empty,
front = add; // place item at front
else
rear->next = add; // else place at rear
rear = add; // have rear point to new node
return true;
}
// Place front item into item variable and remove from queue
template <typename Item>
bool QueueTp<Item>::dequeue(Item & item) {
if (front == NULL)
return false;
item = front->item; // set item to first item in queue
items--;
Node * temp = front; // save location of first item
front = front->next; // reset front to next item
delete temp; // delete former first item
if (items == 0)
rear = NULL;
return true;
}
And here's the header file:
//queuetp.h -- interface for a queue template class
#ifndef QUEUETP_H_
#define QUEUETP_H_
#include <string>
class Worker {
private:
std::string fullname;
long id;
public:
Worker() : fullname("no one"), id(0L) { }
Worker(const std::string & s, long n) : fullname(s), id(n) { }
~Worker() { };
void Set();
void Show() const;
};
template <typename Item>
class QueueTp {
private:
// Node is a nested structure definition local to this class
struct Node { Item item; struct Node * next;};
enum {Q_SIZE = 10};
// private class members
Node * front; // pointer to front of queue
Node * rear; // pointer to rear of queue
int items; // current number of items in queue
const int qsize; // maximum number of items in queue
// preemptive definitions to prevent public copying
QueueTp(const QueueTp & q) : qsize(0) { }
QueueTp & operator=(const QueueTp & q) { return *this;}
public:
QueueTp(int qs = Q_SIZE); // create queue with a qs limit
~QueueTp();
bool isempty() const;
bool isfull() const;
int queuecount() const;
bool enqueue(const Item &item); // add item to end
bool dequeue(Item &item); // remove item from front
};
#endif
Thanks in advance!

New Topic/Question
Reply




MultiQuote




|