// This program uses the a queue template to create a queue of strings.
#include <iomanip>
#include <iostream>
#include <cstdlib>
using namespace std;

//
// TO CREATE A "template" class named "T"
template <class T>
//
// TO CREATE A CLASS NAMED "Dynque"
class Dynque
{
private:

    struct QueueNode
    {
      T value;
      QueueNode *next;
      QueueNode(T val, QueueNode *nextp = NULL)
      {
         value = val; next = nextp;
      }
   };
   QueueNode *front;
   QueueNode *rear;
   int numItems;
public:
   Dynque();
   ~Dynque();
   //
   // FUNCTION PROTOTYPE FOR "engueue"
   // THE FUNCTION DOES NOT RETURN DATA
   // THE PARAMETER LIST CONTAINS "T"
   void enqueue(T);
   // FUNCTION PROTOTYPE FOR "dequeue"
   // THE FUNCTION DOES NOT RETURN DATA
   // THE PARAMETER CONTAINS "T" PASSED BY REFERENCE
   void dequeue(T &);
   //
   //
   bool isEmpty();
   bool isFull();
   void clear();
};


//   Constructor
template <class T>
Dynque<T>::~Dynque()
{
   front = NULL;
   rear = NULL;
   numItems = 0;
}


//   Destructor

template <class T>
Dynque<T>::Dynque()
{
   clear();
}


// Function enqueue inserts the value in val
// at the rear of the queue.

template <class T>
void Dynque<T>::enqueue(T val)
{
   if (isEmpty())
    front = rear = new QueueNode(val);
   else
   {
    rear->next =  new QueueNode(val);
    rear = rear->next;
   }
   numItems++;
}


// Function dequeue removes the value at the
// front of the queue, and copies it into val.

template <class T>
void Dynque<T>::dequeue(T &val)
{
    QueueNode *temp;
    if (isEmpty())
    {
         cout << "The queue is empty.\n";
        exit(1);
      }
    else
    {
       // queue is not empty
       //
       // ASSIGN THE NUMBER AT THE FRONT OF THE LIST
       // TO THE VARIABLE "val"
       val = front ->value;
       //
       temp = front;
       front = front->next;
       delete temp;
       numItems--;
     }
}


// Function isEmpty returns true if the queue
// is empty, and false otherwise.

template <class T>
bool Dynque<T>::isEmpty()
{
   if (numItems)
     return false;
   else
     return true;
}


// Function clear dequeues all the elements
// in the queue.                                 *

template <class T>
void Dynque<T>::clear()
{
   T value;        // Dummy variable for dequeue
   while(!isEmpty())
    dequeue(value);
}

int main()
{
    Dynque<string> queue;
    cout << " ";
    cout << "This program uses a queue to store string data.\n\n";
    cout << "ENTERING STRINGS IN THE QUEUE.";
    //
    // Enqueue 4 strings.
    cout << "\nEnter 4 strings to enqueue:\n\n" ;

    for (int x = 1; x <= 4; x++)
    {
        string value;
         cout << "Enter string "<< x << ": ";
        cin >> value;
        //
        // ADD A VALUE TO THE QUEUE
        queue.enqueue( value );

    }
    cout << "\nREMOVING THE STRINGS FROM THE QUEUE.";
    // Deqeue and retrieve all items in the queue
    cout << "\nThe strings in the queue were:\n";
   cout << "\n";
    while (!queue.isEmpty())
    {
        string value;
        //
        // DELETE A VALUE FROM THE QUEUE
        //queue.dequeue(value);
        cout << "Removing " << value << endl;
    }
    cout << "\n\n";
   system("pause");
    return 0;
}

I can't figure what that error means. It compiles but when I try to run it gives me an error.
1f7eb7aeb6acb61215f93d27005f85f0

When you do this
Dynque<string> queue;
your constructor calls this
clear();
which then calls this
dequeue(value);
which then tries to do this
val = front ->value;
but front is pointing at some random memory somewhere that doesn't belong to you, so you get a segFault (the OS won't let you mess around with memory that isn't yours).

Is your constructor and destructor code swapped around?

Thank you! I moved those lines around. I'm still a novice. I want to get better. The solved code is below.

// This program uses the a queue template to create a queue of strings.
#include <iomanip>
#include <iostream>
#include <cstdlib>
using namespace std;

//
// TO CREATE A "template" class named "T"
template <class T>
//
// CREATE A CLASS NAMED "Dynque"
class Dynque
{
private:

    struct QueueNode
    {
      T value;
      QueueNode *next;
      QueueNode(T val, QueueNode *nextp = NULL)
      {
         value = val; next = nextp;
      }
   };
   QueueNode *front;
   QueueNode *rear;
   int numItems;
public:
   Dynque();
   ~Dynque();
   //
   // FUNCTION PROTOTYPE FOR "engueue"
   // THE FUNCTION DOES NOT RETURN DATA
   // THE PARAMETER LIST CONTAINS "T"
   void enqueue(T);
   // FUNCTION PROTOTYPE FOR "dequeue"
   // THE FUNCTION DOES NOT RETURN DATA
   // THE PARAMETER CONTAINS "T" PASSED BY REFERENCE
   void dequeue(T &);
   //
   //
   bool isEmpty();
   bool isFull();
   void clear();
};


//   Constructor
template <class T>
Dynque<T>::~Dynque()
{

   clear();
}


//   Destructor

template <class T>
Dynque<T>::Dynque()
{
front = NULL;
   rear = NULL;
   numItems = 0;
}


// Function enqueue inserts the value in val
// at the rear of the queue.

template <class T>
void Dynque<T>::enqueue(T val)
{
   if (isEmpty())
    front = rear = new QueueNode(val);
   else
   {
    rear->next =  new QueueNode(val);
    rear = rear->next;
   }
   numItems++;
}


// Function dequeue removes the value at the
// front of the queue, and copies it into val.

template <class T>
void Dynque<T>::dequeue(T &val)
{
    QueueNode *temp;
    if (isEmpty())
    {
         cout << "The queue is empty.\n";
        exit(1);
      }
    else
    {
       // queue is not empty
       //
       // ASSIGN THE NUMBER AT THE FRONT OF THE LIST
       // TO THE VARIABLE "val"
       val = front ->value;
       //
       temp = front;
       front = front->next;
       delete temp;
       numItems--;
     }
}


// Function isEmpty returns true if the queue
// is empty, and false otherwise.

template <class T>
bool Dynque<T>::isEmpty()
{
   if (numItems)
     return false;
   else
     return true;
}


// Function clear dequeues all the elements
// in the queue.                                 *

template <class T>
void Dynque<T>::clear()
{
   T value;        // Dummy variable for dequeue
   while(!isEmpty())
    dequeue(value);
}

int main()
{
    Dynque<string> queue;
    cout << " ";
    cout << "This program uses a queue to store string data.\n\n";
    cout << "ENTERING STRINGS IN THE QUEUE.";
    //
    // Enqueue 4 strings.
    cout << "\nEnter 4 strings to enqueue:\n\n" ;

    for (int x = 1; x <= 4; x++)
    {
        string value;
         cout << "Enter string "<< x << ": ";
        cin >> value;
        //
        // ADD A VALUE TO THE QUEUE
        queue.enqueue( value );

    }
    cout << "\nREMOVING THE STRINGS FROM THE QUEUE.";
    // Deqeue and retrieve all items in the queue
    cout << "\nThe strings in the queue were:\n";
   cout << "\n";
    while (!queue.isEmpty())
    {
        string value;
        //
        // DELETE A VALUE FROM THE QUEUE
        queue.dequeue(value);
        cout << "Removing " << value << endl;
    }
    cout << "\n\n";
   system("pause");
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.