When I try to compile and run this file I get a segmentation fault when the size function is called so I must not have it set up right.
The size function returns the number of stored chars in the queue.
So I am thinking that the size function in my implementation file is not set up correctly.
Keep in mind that I am not finished I am having some other erors as well but when I get this one resolved I will start working on those.
Header file provided by instructor
#ifndef TEMPLATEQ_H
#define TEMPLATEQ_H
#include <iostream>
#include <new>
#include <cstddef>
using namespace std;
class FullTemplateQ // Exception class
{};
class EmptyTemplateQ // Exception class
{};
template<class SomeType> // Node template class
struct QueueNode
{
SomeType data; // Data stored in queue node
QueueNode<SomeType>* nextPtr; // Pointer to next queue node
};
template<class SomeType> // Circular queue template class
class TemplateQ
{
private:
QueueNode<SomeType>* rearPtr; // Pointer to rear of queue
QueueNode<SomeType>* frontPtr; // Pointer to front of queue
void PrintNext(QueueNode<SomeType>* tempPtr) const; // Print trailing items
public:
TemplateQ(); // Default constructor
~TemplateQ(); // Destructor deallocates every node
void Enqueue(SomeType newData); // Adds newdata node to rear of queue
SomeType Dequeue(); // Removes data node from front of queue,
// returning the stored data
bool IsFull() const; // Returns true if queue is full,
// false otherwise
bool IsEmpty() const; // Returns true if queue is empty,
// false otherwise
int Size() const; // Returns the number of items in queue
void ForwardPrint() const; // Prints queue, front to rear
void ReversePrint() const; // Prints queue, rear to front
};
#include "templateq.cpp" // Very Important!! Do not delete!!
#endif
my implementation file
The size function starts on line 58.
#include <new>
#include <cstddef>
#include <iostream>
//There is no need for #include "templateq.h" since the header file has #include "templateq.cpp" at the end of it.
using namespace std;
template<class SomeType>
TemplateQ<SomeType>::TemplateQ()
{
frontPtr = NULL;
rearPtr = NULL;
}
template<class SomeType>
void TemplateQ<SomeType>::Enqueue(SomeType newData)
{
if (IsFull())
throw FullTemplateQ();
else if(IsEmpty())
{
QueueNode<SomeType>* nextQueueLocation;
nextQueueLocation = new QueueNode<SomeType>;
nextQueueLocation->data = newData;
nextQueueLocation->nextPtr = NULL;
if(rearPtr == NULL)
frontPtr = nextQueueLocation;
else
rearPtr->nextPtr = nextQueueLocation;
rearPtr = nextQueueLocation;
}
}
template<class SomeType>
bool TemplateQ<SomeType>::IsFull()const
{
QueueNode<SomeType>* tempPtr;
try
{
tempPtr = new QueueNode<SomeType>;
delete tempPtr;
return false;
}
catch (std::bad_alloc)
{
return true;
}
}
template<class SomeType>
bool TemplateQ<SomeType>::IsEmpty()const
{
return (frontPtr == NULL);
}
template<class SomeType>
int TemplateQ<SomeType>::Size() const
{
int numberOfChars = 0;
QueueNode<SomeType>* countPtr;
if(IsEmpty())
return 0;
while(countPtr != frontPtr)
{
countPtr = rearPtr->nextPtr;
countPtr = countPtr->nextPtr;
numberOfChars ++;
}
return numberOfChars;
}
template<class SomeType>
void TemplateQ<SomeType>::ForwardPrint()const
{
QueueNode<SomeType>* countPtr;
if(IsEmpty())
throw EmptyTemplateQ();
else
{
countPtr = frontPtr;
while(countPtr != rearPtr)
{
countPtr = countPtr->data;
cout << countPtr->data << " ";
countPtr = countPtr->nextPtr;
}
cout << endl;
}
}
template<class SomeType>
void TemplateQ<SomeType>::ReversePrint()const
{}
template<class SomeType>
SomeType TemplateQ<SomeType>::Dequeue()
{
QueueNode<SomeType>* dequeuePtr;
if(IsEmpty())
throw EmptyTemplateQ();
else
{
dequeuePtr = frontPtr;
//dequeuePtr = frontPtr->data;
frontPtr = frontPtr->nextPtr;
if(frontPtr == NULL)
rearPtr = NULL;
}
return dequeuePtr->data;
delete dequeuePtr;
}
template<class SomeType>
TemplateQ<SomeType>::~TemplateQ()
{
QueueNode<SomeType>* tempPtr;
while (frontPtr != NULL)
{
tempPtr = frontPtr;
frontPtr = frontPtr->nextPtr;
delete tempPtr;
}
rearPtr = NULL;
}
my test driver
#include "templateq.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <cstddef>
#include <new>
int main(int argc, char * const argv[])
{
char command, letter;
TemplateQ<char> queue;
ifstream inputs;
if (argc !=2)
{
cout << "Usage:\n program06 <inputfile>\n";
return 1;
}
inputs.open(argv[1]);
if(!inputs)
{
cout << "Unable to open file" << endl;
return 1;
}
inputs >> command;
if(command != 'c')
{
cout << "Invalid File Format - missing 'c'\nTerminating program now..." << endl;
return 1;
}
while(!inputs.eof())
{
switch(command)
{
case 'c':
{
cout << "Constructor()" << endl;
break;
}
case '+':
{
try
{
inputs >> letter;
//cout << letter << endl;
cout << "Enqueue('";
queue.Enqueue(letter);
cout << letter << "')"<< endl;
}
catch(FullTemplateQ)
{
cout << "Failed Full Queue" << endl;
}
break;
}
case '-':
{
try
{
cout << "Dequeue() -- ";
queue.Dequeue();
cout << queue.Dequeue() << endl;
}
catch(EmptyTemplateQ)
{
cout << "Failed Empty Queue" << endl;
}
break;
}
case 'p':
{
cout << "case p" << endl;
break;
}
case 'r':
{
cout << "case r" << endl;
break;
}
case 's':
{
cout << "Size () -- " << queue.Size() << endl;
break;
}
case 'd':
{
queue.~TemplateQ();
cout << "Destructor ()" << endl;
break;
}
default:
{
cout << "Command not recognized" << endl;
}
}
inputs >> command;
}
return 0;
}
this is my output to the terminal window
-bash-3.2$ ./program06 p06input1.txt
Constructor()
Size () -- 0
Enqueue('a')
Enqueue('b')
Enqueue('c')
Enqueue('d')
Enqueue('e')
Enqueue('f')
case p
case r
Segmentation fault
At that segmentation fault is where the size function is supposed to print out the number of stored chars. Can someone tell me what I am doing wrong?