views:

76

answers:

3

when i put the header file in the same folder of the source code. the program doesnt compile. it shows errors. like:7 E:\data structure\asig 5\code.cpp `Queue' has not been declared

driver for queue adt. code.cpp

#include <iostream>
#include "QueueLnk.h"
using namespace std;

// ********************* Function Prototypes ************************
void menu(int& selection);
void processMenu(int selection, Queue<char>& myQueue);
void processEnqueue(Queue<char>& myQueue);
void processDequeue(Queue<char>& myQueue);
void processShow(Queue<char>& myQueue);

// ********************* Function Definitions ***********************
int main()
{
    Queue<char> myQueue;
    int selection;

    do
    {
        menu(selection);
        processMenu(selection, myQueue);
    } while (selection != 4);
    return 0;
}

void menu(int& selection)
// Shows the menu and gets the user's selection.
{
    cout << endl;
    cout << "1. Enqueue an item into the queue" << endl;
    cout << "2. Dequeue an item from the queue" << endl;
    cout << "3. Show the contents of the queue" << endl;
    cout << "4. Exit" << endl;
    cout << endl;
    cout << "Enter your selection: ";
    cin >> selection;
    cin.ignore(80, '\n');
}

void processMenu(int selection, Queue<char>& myQueue)
// Calls the appropriate function based on the user's selection.
{
    switch (selection)
    {
        case 1  : processEnqueue(myQueue);
                  break;
        case 2  : processDequeue(myQueue);
                  break;
        case 3  : processShow(myQueue);
                  break;
        case 4  : cout << "Thanks for using this program!" << endl;
                  break;
        default : cout << "Valid alternatives are 1-4." << endl;
    }
}

void processEnqueue(Queue<char>& myQueue)
// Gets the item's data and adds it to the queue.
{
    char item;

    if (myQueue.isFull())
        cout << "Queue is full!" << endl;
    else
    {
        cout << "Enter the item: ";
        cin >> item;
        cin.ignore(80, '\n');
        myQueue.enqueue(item);
        cout << item << " was added to the queue." << endl;
    }
}

void processDequeue(Queue<char>& myQueue)
// Removes an item from the queue and displays its data.
{
    char item;

    if (myQueue.isEmpty())
        cout << "Queue is empty!" << endl;
    else
    {
        myQueue.dequeue(item);
        cout << item << " was dequeued from the queue." << endl;
    }
}

void processShow(Queue<char>& myQueue)
// Shows the contents of the queue.
{
    char item;
    Queue<char> tempQueue;

    if (myQueue.isEmpty())
        cout << "Queue is empty!" << endl;
    else
    {
        cout << "Contents of the queue:" << endl;
        while (!myQueue.isEmpty())
        {
            myQueue.dequeue(item);
            cout << item << endl;
            tempQueue.enqueue(item);
        }
        while (!tempQueue.isEmpty())
        {
            tempQueue.dequeue(item);
            myQueue.enqueue(item);
        }
    }
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////

[QueueLnk.h]

// Header file for Queue ADT - Linked structure
// By Antonio F. Huertas
// June 18, 2001
// ******************************************************************

#ifndef QUEUE_LNK_H
#define QUEUE_LNK_H

// ********************** Class Definition **************************
template<class ItemType>
class Queue
{
public:
    Queue();                            // Constructor
    ~Queue();                           // Destructor
    bool isEmpty() const;               // Observer
    bool isFull() const;                // Observer
    void enqueue(ItemType newItem);     // Transformer
    void dequeue(ItemType& item);            // Transformer
private:
    struct QueueNode        // Definition of a queue node
    {
        ItemType info;
        QueueNode* next;
    };

    QueueNode* frontPtr;    // Pointer to front item in queue
    QueueNode* rearPtr;     // Pointer to rear item in queue
};

// ****************** Member Function Definitions *******************
template<class ItemType>
Queue<ItemType>::Queue()
// Allocates memory for queue and sets queue to an empty state.
{
    frontPtr = NULL;
    rearPtr = NULL;
}

template<class ItemType>
Queue<ItemType>::~Queue()
// Deallocates memory for the dynamically allocated nodes.
{
    QueueNode* tempPtr;

    while (frontPtr != NULL)
    {
        tempPtr = frontPtr;
        frontPtr = frontPtr->next;
        delete tempPtr;
    }
    rearPtr = NULL;
}

template<class ItemType>
bool Queue<ItemType>::isEmpty() const
// Returns true if the queue is empty; returns false otherwise.
{
    return (frontPtr == NULL);
}

template<class ItemType>
bool Queue<ItemType>::isFull() const
// Returns true if the queue is full; returns false otherwise.
{
    return false;
}

template<class ItemType>
void Queue<ItemType>::enqueue(ItemType newItem)
// Adds new item to the rear of the queue.  Queue must not be full.
{
    QueueNode* newNodePtr;

    newNodePtr = new QueueNode;
    newNodePtr->info = newItem;
    newNodePtr->next = NULL;
    if (frontPtr == NULL)           // Insert in empty queue
        frontPtr = newNodePtr;
    else                            // Insert in non-empty queue
        rearPtr->next = newNodePtr;
    rearPtr = newNodePtr;
}

template<class ItemType>
void Queue<ItemType>::dequeue(ItemType& item)
// Removes front item from queue and returns it.  Queue must not be
// empty.
{
    QueueNode* tempPtr;

    item = frontPtr->info;
    tempPtr = frontPtr;
    frontPtr = frontPtr->next;
    if (frontPtr == NULL)       // If queue is now empty
        rearPtr = NULL;
    delete tempPtr;
}

#endif
A: 

when i put the header file in the same folder of the source code. the program doesnt compile.

and when it does compile? Show us QueueLnk.h

The error says "it is expected that the Queue template class is defined but it is not".

If it does compile when you move QueueLnk.h somewhere else then you should check yours IDE settings - where it does look for file when including one (altough it would be strange if it didn't look for files in current dir).

If it does not compile at all then you should define

template <typename T>
class Queue {
// ...
};

somewhere and include the definition file in code.cpp

lmmilewski
A: 

You might want to remove

using namespace std; 

as there is already a Queue in the std. You will then get loads of errors about cout and cin, prepend these with std::, so they become std::cin and std::cout.

DanDan
`std::queue` is all lowercase, so @alexsniper won't get a clash there. But you are right, polluting the namespace is rarely a good idea. Nor is reinventing STL containers.
Johnsyweb
A: 

bjftuxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

alexsniper