cannot access private member declared in class: queue class template - c++

I have to write code to implement template queue
I get this error : cannot access private member declared in class
at this line
front=front->next;
this is the header file of my code where I get the error:
#include <iostream>
#pragma once
using namespace std;
typedef int Error_code;
#define SUCCESS 0
#define OVERFLOW -1
#define UNDERFLOW -2
template <class T>
class Node{
T item;
Node * next;
Node(){item=0; next=NULL;}
Node(T n){item=n; next=NULL:}
};
template <class T>
class queue
{
protected:
Node<T>* front; // pointer to front of Queue
Node<T> * rear; // pointer to rear of Queue
int count; // current number of items in Queue
public:
queue();
~queue();
bool isempty(){
//return count == 0;
if(front==NULL)
return true;
else
return false;
};
bool isfull(){return false;};
Error_code serve(){
Error_code outcome = SUCCESS;
Node<T> *p;
if(isempty()){
cout<<"empty queue";
outcome=UNDERFLOW;
}
else{
p=front;
front=front->next;
delete p;
count--;
}
return outcome;
} ;
Error_code retrieve(T &item){
Error_code outcome SUCCESS;
if(isempty())
{ // front node is empty, queue is empty
//return false;
cout<<"empty queue";
outcome=UNDERFLOW;
}
return outcome;
};
Error_code append(T item){
Node<T> * n ;
n= new Node; // create node
n->item = item; // set node pointers
n->next = NULL;
if (isempty())
{
rear=front = n;
}
else
{
rear->next = n; // else place at rear
rear = n; // have rear point to new node
}
count++;
return SUCCESS;
};
};

front is a pointer to Node, but next is a private member in Node
template <class T>
class Node{
T item; // since you didn't specify access level
Node * next; // explicitly the access is private by default
Node(){item=0; next=NULL;}
Node(T n){item=n; next=NULL:}
};
so you cannot use it in queue class:
front=front->next; // error
change it to be public or redesign the program

Related

C++ linkedList head returns NULL even though list is not empty

I am trying to implement the pop function of a linked list in C++. My Node and Linked List classes look like this:
//Node class
template <class T>
class Node{
public:
T data;
Node<T> *next;
Node(T data, Node<T> *next);
~Node();
};
template <class T>
Node<T>::Node(T data, Node<T> *next){
this->next = next;
this->data = data;
}
template <class T>
Node<T>::~Node(){
delete this;
}
//LinkedList class
template <class T>
class LinkedList{
public:
//fields
Node<T> *head;
int size;
//methods
LinkedList();
void push(T data);
T pop();
void printList();
};
template <class T>
LinkedList<T>::LinkedList(){
this->head = NULL;
this->size = 0;
}
template <class T>
void LinkedList<T>::printList(){
int i = 1;
while(head){
std::cout<<i<<": "<<head->data<<std::endl;
head = head->next;
i++;
}
}
int main(){
LinkedList<int> list;
for (int i = 1; i < 6; ++i)
list.push(i);
list.printList();
for (int j = 0; j < 3; ++j){
int output=list.pop();
printf("popped: %d\n", output);
}
list.printList();
return 0;
}
Below is my pop function. The problem is that this->head is returning NULL. hence I cannot change its value or access its data field. I used print statements to find out that this->head is returning NULL. How can I resolve this issue?
template <class T>
T LinkedList<T>::pop(){
Node<T> *h = this->head;
//if list is empty
if (this->size==0){
return 0;
}
//if list has only one node
if (this->size==1){
T ret = h->data;
this->head = NULL;
this->size --;
return ret;
}
//if list has multiple nodes
else{
T ret = this->head->data;
this -> head = h->next;
return ret;
}
h.~Node<T>();
}
Below if my push function. I have tested this function and it works fine, but please let me know if it is not handling node pointers properly.
template <class T>
void LinkedList<T>::push(T data){
Node<T> *n = new Node<T>(data, this->head);
this->head = n;
this->size++;
}
template <class T>
Node<T>::~Node(){
delete this;
}
This is so very very wrong. You need to get rid of it completely.
More importantly, printList() is modifying head when it shouldn't be. That is why head is NULL when pop() is called. Use a local Node* variable to iterate the list:
template <class T>
void LinkedList<T>::printList(){
int i = 1;
Node<T> *n = head; // <-- here
while(n){
std::cout << i << ": " << n->data << std::endl;
n = n->next;
i++;
}
}
Also, pop() is not freeing nodes correctly (if at all), and not always decrementing size. It should look more like this instead:
template <class T>
T LinkedList<T>::pop(){
Node<T> *h = this->head;
//if list is empty
if (this->size==0){
return T(); // <-- not 0! or throw an exception instead...
}
//if list has only one node
if (this->size==1){
T ret = h->data;
this->head = NULL;
this->size--;
delete h; // <-- add this!
return ret;
}
//if list has multiple nodes
T ret = this->head->data;
this->head = h->next;
this->size--; // <-- add this!
delete h; // <-- not h.~Node<T>()!
return ret; // <-- moved down here!
}
Or simpler, this (no need to handle the size==1 case separately):
template <class T>
T LinkedList<T>::pop(){
Node<T> *h = this->head;
//if list is empty
if (!h){
return T();
}
//if list has any nodes
T ret = h->data;
this->head = h->next;
this->size--;
delete h;
return ret;
}

C++ Queue from Linked List

Good evening. I have been trying to implement a Queue class in C++, taking a previously created Linked List class as a base.
Linked Linked List:
#include <cstddef>
#include <iostream>
#include <cstdio>
using namespace std;
template <class T>
class LinkedList {
public:
LinkedList() {
head = NULL;
}
~LinkedList() {
MakeEmpty();
}
struct Node {
T value;
Node *next;
};
Node* getHead() {
return head;
}
void Print();
void Insert();
void MakeEmpty();
private:
Node *head; // Head of the linked list.
};
Queue class:
#include "LinkedList.h"
template <class T>
class Queue {
public:
Queue() {
LinkedList<T>::Node *tnode = Q.getHead();
}
~Queue() {
Q.MakeEmpty();
}
void Enqueue( T x ) {
LinkedList<T>::Node *cnode = Q.getHead();
//Find the last element of Q
while( cnode -> next != NULL ) {
cnode = cnode -> next;
}
//Add x to the end of the queue
Q.Insert( x );
}
void Dequeue() {
LinkedList<T>::Node *hnode = Q.getHead();
//Rest of function
}
void Print() {
Q.PrintList();
}
private:
LinkedList<T> Q;
};
As you probably noticed, I am making them template classes. When compiling, I am told that tnode (found in the constructor of the Queue class) has not been declared in the scope. Any suggestions on how to fix this?
EDIT 1: The error message that I am getting is:
RCQueue.h: In constructor ‘Queue::Queue()’:
RCQueue.h:8:28: error: ‘tnode’ was not declared in this scope
LinkedList::Node *tnode = Q.getHead();
The main purpose of my constructor is to initialize the "head" pointer from the LinkedList class as NULL. I was also curious as to how one could go about declaring a variable of a structure that was declared in another template class.
Enqueue Algorithm :
1. Create a newNode with data and address.
2. if queue i.e front is empty
i. front = newnode;
ii. rear = newnode;
3. Else
i.rear->next = newnode;
ii.rear = newnode;
Dequeue Algorithm :
1. if queue is i.e front is NULL printf("\nQueue is Empty \n");
2. Else next element turn into front
i. struct node *temp = front ;
ii. front = front->next;
iii.free(temp);
C++ implementation :
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node *next;
};
node *front = NULL;
node *rear =NULL;
void Enque(int data)
{
node *newnode = new node;
newnode->data = data;
newnode ->next = NULL;
if(front==NULL)
{
front=newnode;
rear=newnode;
}
else
{
rear->next = newnode;
rear = newnode;
}
}
void Deque()
{
struct node *temp;
if (front == NULL)
{
printf("\nQueue is Empty \n");
return;
}
else
{
temp = front;
front = front->next;
if(front == NULL) rear = NULL;
free(temp);
}
}
void display()
{
node *temp=front;
if(front==NULL)
{
printf("\nQueue is Empty \n");
}
else
{
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
}
cout<<endl;
}
You need typename in front of every use of the Node type in LinkedList that you reference within Queue because its dependent on the template parameter T. In specific,
template <class T>
class Queue {
public:
Queue() {
typename LinkedList<T>::Node *tnode = Q.getHead();
}
~Queue() {
Q.MakeEmpty();
}
void Enqueue( T x ) {
typename LinkedList<T>::Node *cnode = Q.getHead();
//Find the last element of Q
while( cnode -> next != NULL ) {
cnode = cnode -> next;
}
//Add x to the end of the queue
Q.Insert( x );
}
void Dequeue() {
typename LinkedList<T>::Node *hnode = Q.getHead();
//Rest of function
}
void Print() {
Q.PrintList();
}
private:
LinkedList<T> Q;
};
Notice the addition of typename before the uses of LinkedList<T>::Node.
Of course you'll also hear complaints about the missing definition of MakeEmpty() within LinkedList that is called in your Queue class, so just add a definition for it.
For more information on why typename is needed, this post explains its pretty clearly.

C++ Disjoint Set Implementation with Doubly Linked Lists

Can someone help me find/make/point me in the right direction of a disjoint set implementation using Doubly Linked Lists? I have some supplemental code and I've already started editing it, I was wondering if someone could help me fill the rest in or at least give me some pointers on how to do so.
DisjointSet.h
#pragma once
#include "TemplateDoublyLinkedList.h"
#include <cstddef>
#include <iostream>
#include <vector>
using namespace std;
// Disjoint Set
template <typename T>
class DisjointSet {
private:
vector<DListNode<T>*> nodeLocator;
public:
~DisjointSet();
DisjointSet(int n) { nodeLocator.resize(n); }
vector<DListNode<T>*> getNodeLocator() const { return nodeLocator; }
DListNode<T>* MakeSet(int key, T node);
DListNode<T>* Union(DListNode<T> nodeI, DListNode<T> nodeJ);
DListNode<T>* FindSet(DListNode<T> node);
DListNode<T>* FindSet(int nodeKey);
};
template <typename T>
DListNode<T>* DisjointSet<T>::MakeSet(int key, T node)
{
DListNode<T> *temp = new DListNode(key, node);
nodeLocator.insert(nodeLocator.begin() + key - 1, temp);
return temp;
}
template <typename T>
DListNode<T>* DisjointSet<T>::Union(DListNode<T> nodeI, DListNode<T> nodeJ){
if (nodeI.getListSize() >= nodeJ.getListSize())
{
nodeI.getTrailer()->setNext(nodeJ.getRepresentative());
nodeJ.getRepresentative()->setPrevious(nodeI.getTrailer());
nodeI.getRepresentative()->setTrailer(nodeJ.getTrailer());
nodeJ.getRepresentative()->setTrailer(NULL);
nodeJ.getRepresentative()->setRepresentative(nodeI.getRepresentative());
nodeI.setListSize(nodeI.getListSize()+nodeJ.getListSize());
return nodeI.getRepresentative();
}
else if (nodeI.getListSize() < nodeJ.getListSize())
{
nodeJ.getTrailer()->setNext(nodeI.getRepresentative());
nodeI.getRepresentative()->setPrevious(nodeJ.getTrailer());
nodeJ.getRepresentative()->setTrailer(nodeI.getTrailer());
nodeI.getRepresentative()->setTrailer(NULL);
nodeI.getRepresentative()->setRepresentative(nodeJ.getRepresentative());
nodeJ.setListSize(nodeI.getListSize() + nodeJ.getListSize());
return nodeJ.getRepresentative();
}
return NULL;
}
template <typename T>
DListNode<T>* DisjointSet<T>::FindSet(DListNode<T> node){
}
template <typename T>
DListNode<T>* DisjointSet<T>::FindSet(int nodeKey){
if (nodeLocator[nodeKey-1]) != NULL)
return nodeLocator[nodeKey-1];
else
return nullptr;
}
template <typename T>
ostream& DisjointSet<T>::operator<<(ostream& out, const DisjointSet<T>& ds){
}
TemplateDoublyLinkedList.h
#pragma once
#include <string>
#include <cstdlib>
#include <iostream>
#include <stdexcept>
using namespace std;
// list node
template <typename T>
class DListNode {
private:
friend class DisjointSet;
int key, listSize;
T obj;
DListNode *prev, *next, *representative;
DListNode *trailer; //just the representative node has this pointer assigned
public:
DListNode(int k, T e, DListNode *p = NULL, DListNode *n = NULL)
: key(k), obj(e), prev(p), next(n) { listSize = 1; }
T getElem() const { return obj; }
T& getElemt() { return obj; }
DListNode<T> * getNext() const { return next; }
DListNode<T> * getPrev() const { return prev; }
void setNext(DListNode* n) { this->next = n; }
void setPrevious(DListNode* p) { this->prev = p; }
DListNode<T>* insert_before(T d); // insert the int before this node
// return a pointer to the inserted node
DListNode<T>* insert_after(T d); // insert the int after this node
// return a pointer to the inserted node
void delete_before(); // delete the node before this node
void delete_after(); // delete the node after this node
int getKey() { return key; }
DListNode<T>* getRepresentative() const { return representative; }
DListNode<T>* getTrailer() const { return trailer; }
void setRepresentative(DListNode* rep);
void setTrailer(DListNode* trail);
int getListSize() { return this->getRepresentative()->listSize; }
void setListSize(int lSize)
{ this->listSize = lSize; if(this->next != NULL) this->next->setListSize(lSize); }
};
template <typename T>
void DListNode<T>::setRepresentative(DListNode* rep) {
this->representative = rep;
if (this->next != NULL)
{
this->next->setRepresentative(rep);
}
}
template <typename T>
void DListNode<T>::setTrailer(DListNode* trail) {
this->representative = trail;
}
template <typename T>
DListNode<T>* DListNode<T>::insert_before(T d) {
DListNode<T>* temp = new DListNode(d); //temp pointer // 1 OPERATION
if(prev!= NULL){ //if previous is not null // 2 OPERATION
temp -> next = this; //initialize next // 2 OPERATION
temp -> prev = prev; //initialize previous // 2 OPERATION
prev -> next = temp; // insert in the list // 2 OPERATION
prev = temp; // 1 OPERATION
}
else{ //if null than do a simple insert
temp -> next = this; // 2 OPERATION
prev = temp; // 1 OPERATION
}
return temp; // 1 OPERATION
}
template <typename T>
DListNode<T>* DListNode<T>::insert_after(T d) {
DListNode<T>* temp = new DListNode(d); // 2 OPERATION
if(next != NULL){ //if next is not null do this // 2 OPERATION
temp -> next = next; //initialize next and previous // 2 OPERATION
temp -> prev = this; // 2 OPERATION
next -> prev = temp; //make the connection to insert // 2 OPERATION
next = temp; // 1 OPERATION
}
else{ //if null than do a simple insert
temp -> prev = this; // 2 OPERATION
next = temp; // 1 OPERATION
}
return temp; // 1 OPERATION
}
template <typename T>
void DListNode<T>::delete_before() {
if(prev!= NULL){ //check if something is there // 2 OPERATION
DListNode<T>* temp = prev; // 1 OPERATION
temp -> prev-> next = temp-> next; //change the interconnections // 4 OPERATION
temp -> next -> prev = temp -> prev; // 4 OPERATION
delete temp; //delete the element // 1 OPERATION
}
else{
cout << ">Error: Nothing is there :(" << endl;
}
}
template <typename T>
void DListNode<T>::delete_after() {
if(next != NULL){ //check if something is there
DListNode<T>* temp = next; // 1 OPERATION
next -> prev = this; //change the interconnections // 2 OPERATION
next = next -> next; // 2 OPERATION
delete temp; //delete the element // 1 OPERATION
}
else{
cout << ">Error: Nothing is there :(" << endl;
}
}

Bubble sort a doubly linked list

I've gone through a bunch of threads trying to understand what is going on exactly with linked lists and bubblesort, and I think I get the bulk of it.
Right now my program is simply crashing when I get to the sort function and I am not sure why. Hopefully another set of eyes will see what I do not.
Any help is greatly appreciated.
DoublyList.h:
#include "listNode.h"
#ifndef DOUBLYLIST_H
#define DOUBLYLIST_H
template <typename T>
class DoublyList
{
public:
DoublyList();
~DoublyList();
void addFront(T d);
void addBack(T d);
T removeFront();
T removeBack();
T peak();
bool isEmpty();
int getSize();
void printList();
void sortList();
private:
ListNode<T> *front;
ListNode<T> *back;
int numOfElements;
};
template <typename T>
DoublyList<T>::DoublyList(){
front = NULL;
back = NULL;
numOfElements = 0;
}
template <typename T>
DoublyList<T>::~DoublyList(){
if(numOfElements!=0){
ListNode<T> *current;
current = front;
while (current != back)
{
ListNode<T> *temp = current;
current = current->next;
temp->next = NULL;
temp->prev = NULL;
delete temp;
numOfElements--;
}
//at this point current = back, now delete it
current->next = NULL;
current->prev = NULL;
delete current;
numOfElements--;
}
//this is a safeguard if you create a LL and then delete it without doing anything to it
else{
cout<<"deleted empty LL"<<endl;
delete front;
delete back;
}
}
template <typename T>
void DoublyList<T>::addFront(T d)
{
ListNode<T> *node = new ListNode<T>();
node->data = d;
if (isEmpty()){
back = node;
}
else{
front->prev = node;
}
node->next = front;
front = node;
++numOfElements;
}
template <typename T>
T DoublyList<T>::removeFront()
{
if (isEmpty()){
return T();
}
else
{
ListNode<T>* temp = front;
if (front->next == 0){
back = 0;
}
else
{
front->next->prev = 0;
}
front = front->next;
temp->next = 0;
T theData = temp->data;
delete temp;
--numOfElements;
return theData;
}
}
template <typename T>
void DoublyList<T>::addBack(T d)
{
ListNode<T> *node = new ListNode<T>();
node->data = d;
if (isEmpty()){
front = node;
}
else{
back->next = node;
}
node->prev = back;
back = node;
++numOfElements;
}
template <typename T>
T DoublyList<T>::removeBack()
{
if (isEmpty()) {
return T();
}
else
{
ListNode<T>* temp;
temp = back;
if (back->prev == 0){
front = 0;
}
else{
back->prev->next = 0;
}
back = back->prev;
temp->prev = 0;
T theData = temp->data;
delete temp;
--numOfElements;
return theData;
}
}
template <typename T>
T DoublyList<T>::peak()
{
if (isEmpty()) {
return T();
}
return front->data;
}
template <typename T>
int DoublyList<T>::getSize(){
return numOfElements;
}
template <typename T>
bool DoublyList<T>::isEmpty(){
if(numOfElements == 0){
return true;
}
else{
return false;
}
}
template <typename T>
void DoublyList<T>::printList(){
if(numOfElements!=0){
ListNode<T> *current = front;
while(current!=back)
{
cout<<current->data<<endl;
current = current->next;
}
cout<<back->data<<endl;
}
else{
cout<<"list is empty"<<endl;
}
}
template <typename T>
void DoublyList<T>::sortList(){
int size = getSize();
ListNode<T> *current;
ListNode<T> *dummy;
ListNode<T> *next;
if(current == NULL) return;
if(current -> next == NULL) return;
int swapped = 1;
while(swapped){
swapped = 0; //last pass unless there is a swap
while(current -> next != NULL){
if(current-> data < current -> next -> data){
swapped = 1; //swap, will need to re-enter while loop
//actual number swap
dummy -> data = current -> data;
current -> data = current -> next -> data;
current -> next -> data = dummy -> data;
}
current = current -> next;
}
}
}
#endif
listNode.h:
#include <iostream>
#ifndef LISTNODE_H
#define LISTNODE_H
using namespace std;
template <typename T>
class ListNode
{
public:
T data;//the data that we will store
ListNode();
ListNode(int d);
~ListNode();
ListNode *next;//int and ptr and the member variables
ListNode *prev;
};
template <typename T>
ListNode<T>::ListNode(int d){
data = d;
next = NULL;
prev = NULL;
}
template <typename T>
ListNode<T>::ListNode(){}
template <typename T>
ListNode<T>::~ListNode(){
delete next;
delete prev;
cout<<"deleted Node"<<endl;
}
#endif
testList.cpp
#include <iostream>
#include "doublyList.h"
#include "genericQueue.h"
int main(){
DoublyList<int> testQueue;
testQueue.addBack(3);
testQueue.addBack(5);
testQueue.addBack(2);
testQueue.addBack(10);
testQueue.addBack(1);
cout << "Before Sort: " << endl;
testQueue.printList();
cout << "After Sort: " << endl;
testQueue.sortList();
testQueue.printList();
}
The erors I could find so far are:
Your default ListNode() constructor doesn't null the next and prev pointers.
In void DoublyList<T>::sortList() you don't initialize dummy, so it just points into nowhere. Actually there is no reason to use a node list at all, you can just directly use a variable of type T.
You don't initialize current in the same function and you actually should reset current to e.g. front at the beginning of each outer loop.
You don't use next at all (and don't need to), so just remove it.
To sum it up, this is what void DoublyList<T>::sortList() could look like:
template <typename T>
void DoublyList<T>::sortList(){
int size = getSize();
ListNode<T> *current=front;
T dummy;
if (current == NULL) return;
if (current->next == NULL) return;
int swapped = 1;
while (swapped){
current = front;
swapped = 0; //last pass unless there is a swap
while (current->next != NULL){
if (current->data < current->next->data){
swapped = 1; //swap, will need to re-enter while loop
//actual number swap
dummy = current->data;
current->data = current->next->data;
current->next->data = dummy;
}
current = current->next;
}
}
}
and this is my suggestion for the ListNode constructor.
template <typename T>
ListNode<T>::ListNode() :
next(nullptr),
prev(nullptr),
data{}
{}
Besides that, I also agree with DaveB that swapping pointers is the approach you should actually use.
To start with you need to initialize current in your sort function,
current = first;
template <typename T>
void DoublyList<T>::sortList(){
ListNode<T> *current;
ListNode<T> *next;
T tmp;
current = front;
if(current == NULL) return;
if(current -> next == NULL) return;
int swapped = 1;
while(swapped){
swapped = 0; //last pass unless there is a swap
while(current->next != nullptr){
if(current->data < current->next->data){
swapped = 1; //swap, will need to re-enter while loop
//actual number swap
tmp = current->data;
current->data = current->next->data;
current->next->data = tmp;
}
current = current -> next;
}
if (swapped) // go back to start of list for next pass
current = front;
}
}

template queue getting wrong results

I am writing a c++ code to implement template queue, but when I run my code, I get wrong results, not the expected results, here is the header file and the main code.
queue.h
#include <iostream>
#pragma once
using namespace std;
typedef int Error_code;
#define SUCCESS 0
#define OVERFLOW -1
#define UNDERFLOW -2
template <class T>
class Node{
public:
T item;
Node * next;
Node(){item=0; next=NULL;}
Node(T n){item=n; next=NULL;}
};
template <class T>
class queue
{
protected:
Node *front, *rear;
int count;
public:
queue(){
cout<<"constructor \n";
count = 0;
front = rear = NULL;};
~queue(){Node<T> * p;
while(front != NULL)
{
p = front;
front = front->next;
delete p;
}};
bool isempty(){
//return count == 0;
if(front==NULL)
return true;
else
return false;
};
bool isfull(){return false;};
Error_code serve(){
Error_code outcome = SUCCESS;
Node<T> *p;
if(isempty()){
cout<<"empty queue\n";
outcome=UNDERFLOW;
}
else{
p=front;
front=front->next;
delete p;
count--;
}
return outcome;
} ;
Error_code retrieve(T &item){
Error_code outcome= SUCCESS;
if(isempty())
{ // front node is empty, queue is empty
//return false;
cout<<"empty queue\n";
outcome=UNDERFLOW;
}
return outcome;
};
Error_code append(T item){
Node<T> * n ;
n= new Node; // create node
n->item = item; // set node pointers
n->next = NULL;
if (isempty())
{
rear=front = n;
}
else
{
rear->next = n; // else place at rear
rear = n; // have rear point to new node
}
count++;
return SUCCESS;
};
};
main.cpp
#include "queue.h"
#include <iostream>
using namespace std;
int main()
{
queue<int> q;
queue<char> q2;
int x;
char y;
cout<<"the fisrt queue is :";
q.isempty()?cout<<"empty \n\n":cout<<"not empty \n\n";
cout<<"the second queue is :";
q2.isempty()?cout<<"empty \n\n":cout<<"not empty \n\n";
q.append(2);
q.append(3);
q.append(4);
q2.append('a');
q2.append('b');
q2.append('c');
q.retrieve(x);
cout<<x<<endl;
q.serve();
q.retrieve(x);
cout<<x<<endl;
q.serve();
q.retrieve(x);
cout<<x<<endl;
q.serve();
q2.retrieve(y);
cout<<y<<endl;
q2.serve();
q2.retrieve(y);
cout<<y<<endl;
q2.serve();
q2.retrieve(y);
cout<<y<<endl;
q2.serve();
q.retrieve(x);
q2.retrieve(y);
system("pause");
return 0;
}
I get these results:
constructor
constructor
the first queue is: empty
the second queue is: empty
-858993460
-858993460
-858993460
then it prints some symbols
empty queue
empty queue
Your retrieve function doesn't retrieve:
Error_code retrieve(T &item){
Error_code outcome= SUCCESS;
if(isempty())
{ // front node is empty, queue is empty
//return false;
cout<<"empty queue\n";
outcome=UNDERFLOW;
}else
{
// needs something like:
item = front->item;
}
return outcome;
};
Apart from the missing template arguments, it should work then.