| | |
Please help Queue distructor problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 31
Reputation:
Solved Threads: 0
yea I know I spelled destructor wrong what can I say my fingers got in the way of my typing
I have scoured the internet and tried several different ways to implement the destructor on my link list queue.
I highlighted the troulesome code like this
//*****************************************
code
//*****************************************
here is the program.
When I compile and run it I get a segmentation fault.
This is the file I am trying to run
And this is my output
but my code seems to work with this file
and the output with this file is this
The program works up to the destructor code.
can someone give me a hand?
I have scoured the internet and tried several different ways to implement the destructor on my link list queue.
I highlighted the troulesome code like this
//*****************************************
code
//*****************************************
here is the program.
c++ Syntax (Toggle Plain Text)
#include <new> #include <cstddef> #include <iostream> 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 (frontPtr != NULL) { rearPtr->nextPtr = new QueueNode<SomeType>; rearPtr = rearPtr->nextPtr; rearPtr->data = newData; } else { rearPtr = new QueueNode<SomeType>; rearPtr->data = newData; rearPtr->nextPtr = NULL; frontPtr = rearPtr; } } 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 = 1; QueueNode<SomeType>* countPtr = frontPtr; if (IsEmpty()) return 0; while(countPtr != rearPtr) { countPtr = countPtr->nextPtr; numberOfChars ++; } return numberOfChars; } template<class SomeType> void TemplateQ<SomeType>::ForwardPrint()const { QueueNode<SomeType>* printPtr = frontPtr; if(IsEmpty()) throw EmptyTemplateQ(); else { while(printPtr != rearPtr) { cout << printPtr->data << " "; printPtr = printPtr->nextPtr; } cout << printPtr->data; } } template<class SomeType> void TemplateQ<SomeType>::ReversePrint()const { if(IsEmpty()) throw EmptyTemplateQ(); PrintNext(frontPtr); } template<class SomeType> void TemplateQ<SomeType>::PrintNext(QueueNode<SomeType>* tempPtr) const { if (tempPtr != rearPtr) PrintNext(tempPtr->nextPtr); cout << tempPtr->data << " "; } template<class SomeType> SomeType TemplateQ<SomeType>::Dequeue() { if(IsEmpty()) throw EmptyTemplateQ(); else { SomeType removeData = frontPtr->data; QueueNode<SomeType>* dequeuePtr = frontPtr; frontPtr = frontPtr->nextPtr; delete dequeuePtr; if(frontPtr == NULL) rearPtr = NULL; return removeData; } } //*********************This is the troublesome function*********** template<class SomeType> TemplateQ<SomeType>::~TemplateQ() { while(frontPtr != rearPtr) { QueueNode<SomeType>* dequeuePtr = frontPtr; frontPtr = frontPtr->nextPtr; delete dequeuePtr; } } //*********************************************************************
This is the file I am trying to run
C++ Syntax (Toggle Plain Text)
c s - + w + x + y r s d c - - p + g + h + i p r s - - - - s d
And this is my output
C++ Syntax (Toggle Plain Text)
-bash-3.2$ ./program06 p06input2.txt Constructor() Size () -- 0 Dequeue() -- Failed Empty Queue Enqueue('w') Enqueue('x') Enqueue('y') Reverse Print() -- y x w Size () -- 3 Destructor () Constructor() Dequeue() -- y Segmentation fault -bash-3.2$ gedit
but my code seems to work with this file
C++ Syntax (Toggle Plain Text)
c s - p + a + b + c + d + e + f p r s - - p + g + h + i p r s - - - - - - - - s d
and the output with this file is this
C++ Syntax (Toggle Plain Text)
-bash-3.2$ ./program06 p06input1.txt Constructor() Size () -- 0 Dequeue() -- Failed Empty Queue Forward Print() -- Failed Empty Queue Enqueue('a') Enqueue('b') Enqueue('c') Enqueue('d') Enqueue('e') Enqueue('f') Forward Print() -- a b c d e f Reverse Print() -- f e d c b a Size () -- 6 Dequeue() -- a Dequeue() -- b Forward Print() -- c d e f Enqueue('g') Enqueue('h') Enqueue('i') Forward Print() -- c d e f g h i Reverse Print() -- i h g f e d c Size () -- 7 Dequeue() -- c Dequeue() -- d Dequeue() -- e Dequeue() -- f Dequeue() -- g Dequeue() -- h Dequeue() -- i Dequeue() -- Failed Empty Queue Size () -- 0 Destructor ()
can someone give me a hand?
Last edited by JustLearning; Oct 24th, 2008 at 1:01 am.
I dont get it,
Your template destructor doesnt have anything to delete when the size of the class is 0,
So i guess you should just implement to apply to an empty stack too.
if *pointer==null;//maybe;
Your template destructor doesnt have anything to delete when the size of the class is 0,
So i guess you should just implement to apply to an empty stack too.
if *pointer==null;//maybe;
•
•
Join Date: Sep 2008
Posts: 31
Reputation:
Solved Threads: 0
Thanks for looking at it but I figured it out finally.
heres what I did.
heres what I did.
c++ Syntax (Toggle Plain Text)
while(rearPtr != frontPtr) { QueueNode<SomeType>* dequeuePtr = frontPtr; frontPtr = frontPtr->nextPtr; delete dequeuePtr; if(frontPtr == rearPtr) { rearPtr = NULL; frontPtr = NULL; } }
•
•
Join Date: May 2008
Posts: 54
Reputation:
Solved Threads: 1
you already have a dequeue function
why not use such a loop
why not use such a loop
C++ Syntax (Toggle Plain Text)
while(!isempty()) { dequeue(); }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Vector segment fault
- Next Thread: Need help: Running every 30 secs.
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





