I am having trouble with my print function for my linked queue. My compiler error is pointing towards
cout << tempPtr->info << " "; line 149
tempPtr = tempPtr->next; line 150
in my print function. Can anyone help me fix this? Thanks

CODE:

#include <iostream>
#include <cstddef>		// For NULL
#include <new>			// For bad_alloc
using namespace std;

template < class ItemType > struct NodeType {
    ItemType info;
     NodeType < ItemType > *next;
    ;
};

template < class ItemType > class QueType {

  public:
    QueType();			// CONSTRUCTOR 
    ~QueType();			// DESTRUCTOR 
    bool IsEmpty() const;
    bool IsFull() const;
    void Enqueue(ItemType newItem);
    void Dequeue(ItemType & item);
    void MakeEmpty();
    void Print();
    QueType(const QueType & anotherQue);
  private:
    NodeType < ItemType > *qFront;
    NodeType < ItemType > *qRear;
    ;
};


template < class ItemType > QueType < ItemType >::QueType()	// CONSTRUCTOR 
{
    qFront = NULL;
    qRear = NULL;
}

template < class ItemType > QueType < ItemType >::~QueType()	// destructor 
{
    MakeEmpty();
}



template < class ItemType > bool QueType < ItemType >::IsEmpty() constconst
{
    return (qFront == NULL);
}


template < class ItemType > void QueType < ItemType >::Enqueue(ItemType newItem)
// Adds newItem to the rear of the queue. 
// Pre: Queue has been initialized. 
// Queue is not full. 
// Post: newItem is at rear of queue. 
{
    NodeType < ItemType > *ptr;


    ptr = new NodeType < ItemType >;
    ptr->info = newItem;
    ptr->next = NULL;
    if (qRear == NULL)
	qFront = ptr;
    else
	qRear->next = ptr;
    qRear = ptr;
}


template < class ItemType > void QueType < ItemType >::Dequeue(ItemType & item)
// Removes element from from front of queue 
// And returns it in item. 
// Pre: Queue has been initialized. 
// Queue is not empty. 
// Post: Front element has been removed from queue. 
// Item is a copy of removed element. 
{
    NodeType < ItemType > *newNodePtr;
    newNodePtr = new NodeType < ItemType >;
    newNodePtr->info = item;
    newNodePtr->next = NULL;
}



template < class ItemType > void QueType < ItemType >::MakeEmpty()
// Post: Queue is empty; all elements deallocated. 
{
    NodeType < ItemType > *tempPtr;
    while (qFront != NULL) {
	tempPtr = qFront;
	qFront = qFront->next;
	delete tempPtr;
    }
    qRear = NULL;

}




template < class ItemType > bool QueType < ItemType >::IsFull() constconst
// Returns true if there is no room for another NodeType 
// Node on the free store; false otherwise. 
{
    NodeType < ItemType > *location;
    try {
	location = new NodeType < ItemType >;
	delete location;
	return false;
    }
    catch(bad_alloc exception) {
	return true;
    }
}


template < class ItemType > QueType < ItemType >::QueType(const QueType & anotherQue)
{
    NodeType < ItemType > *ptr1;
    NodeType < ItemType > *ptr2;

    if (anotherQue.qFront == NULL)
	qFront = NULL;
    else {
	qFront = new NodeType < ItemType >;
	qFront->info = anotherQue.qFront->info;
	ptr1 = anotherQue.qFront->next;
	ptr2 = qFront;
	while (ptr1 != NULL) {
	    ptr2->next = new NodeType < ItemType >;
	    ptr2 = ptr2->next;
	    ptr2->info = ptr1->info;
	    ptr1 = ptr1->next;
	}
	ptr2->next = NULL;
	qRear = ptr2;
    }
}

template < class ItemType > void QueType < ItemType >::Print()
{
    NodeType < ItemType > *tempPtr;
    if (!IsEmpty()) {
	tempPtr = qRear->next;
	cout << "The queue contains: ";
	while (tempPtr != qRear) {

	    cout << tempPtr->info << " ";
	    tempPtr = tempPtr->next;
	}

	cout << tempPtr->info << endl << endl;
    } else
	cout << "The queue is empty." << endl << endl;
}



int main()
{
    QueType < int >IntQueue;
    int x;
    IntQueue.MakeEmpty();
    IntQueue.Dequeue(x);
    IntQueue.Enqueue(10);
    IntQueue.Enqueue(20);
    IntQueue.Enqueue(30);
    IntQueue.Enqueue(40);

    IntQueue.Dequeue(x);

    cout << "The int queue contains: " << endl;
    IntQueue.Print();
    if (IntQueue.IsFull() == false)
	cout << "The int queue is not full !" << endl;
    else
	cout << "The int queue is full !" << endl;

    QueType < float >FloatQueue;
    float y;
    FloatQueue.MakeEmpty();
    FloatQueue.Dequeue(y);
    FloatQueue.Enqueue(7.1);

    FloatQueue.Enqueue(2.3);

    FloatQueue.Enqueue(3.1);
    FloatQueue.Dequeue(y);
    cout << "The float queue contains: " << endl;
    FloatQueue.Print();
    QueType < float >FloatQueue2 = FloatQueue;
    cout << "The float queue 2 contains: " << endl;
    FloatQueue2.Print();
    FloatQueue.MakeEmpty();
    cout << "The float queue 3 contains: " << endl;
    FloatQueue2.Print();


    system("pause");
    return 0;
}

I did not get those errors when I compiled your program. There are 2 extra semicolons around and you have probably pasted an extra const on two lines. Exception is a class within the std:: namespace so you can't use it as a variable name.

What is the specific error the compiler is reporting?

My program does run but there is a break point at the line

cout << tempPtr->info << " ";

I am not entirely sure how to fix this.

Wherever you set qRear initially (I didn't trace your code all the way back), qRear->next is null.

This article has been dead for over six months. Start a new discussion instead.