Nov 3, 2011 at 3:01am Nov 3, 2011 at 3:01am UTC
Why I am getting a segmentation fault in my program? I think its a problem with my push function being stuck in the decision statement. How can I fix this?
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
#include <stdexcept>
#include <cstdlib>
template <class T>
struct QNode
{
QNode(const T&);
//Data members
T data;
QNode<T> *next;
};
//Method definitions for the QNode class
template <class T>
QNode<T>::QNode(const T& newData)
{
data = newData
next = NULL;
}
// Forward declaration of the Queue template class
template <class T>
class Queue;
// Forward declaration of the operator<< template function
template <class T>
std::ostream& operator <<(std::ostream&, const Queue<T>&);
template <class T>
class Queue
{
// friend declaration for the template function - note the
// special syntax
friend std::ostream& operator << <>(std::ostream&, const Queue<T>&);
public :
Queue();
// ~Queue();
// Queue(const Queue<T>&) copy constructor?
int size() const ;
bool empty() const ;
// void clear();
// T& front() throw(out_of_range);
// T& back() throw(out_of_range);
void push(const T&);
// void pop();
//Operators to overload
//assignment operator
private :
QNode<T> *qFront;
QNode<T> *qRear;
};
// Method definitions for the Queue class
template <class T>
std::ostream& operator <<(std::ostream& out, const Queue<T>& rightOp)
template <class T>
class Queue
{
// friend declaration for the template function - note the
// special syntax
friend std::ostream& operator << <>(std::ostream&, const Queue<T>&);
public :
Queue();
// ~Queue();
// Queue(const Queue<T>&) copy constructor?
int size() const ;
bool empty() const ;
// void clear();
// T& front() throw(out_of_range);
// T& back() throw(out_of_range);
void push(const T&);
// void pop();
//Operators to overload
//assignment operator
private :
QNode<T> *qFront;
QNode<T> *qRear;
};
// Method definitions for the Queue class
template <class T>
std::ostream& operator <<(std::ostream& out, const Queue<T>& rightOp)
{
int i;
for (i = 0; i < rightOp.size(); i++)
out << " " << rightOp;
return out;
}
template <class T>
Queue<T>::Queue()
{
qFront = NULL;
qRear = NULL;
}
template <class T>
int Queue<T>::size() const
{
int count = 0;
QNode<T> *ptr;
ptr = qFront;
while (ptr != NULL)
{
count++;
ptr = ptr->next;
}
return count;
}
template <class T>
bool Queue<T>::empty() const
{
if (qFront == NULL)
return true ;
else
return false ;
}
template <class T>
void Queue<T>::push(const T& item)
{
QNode<T> *ptr, *lagPtr;
ptr = new QNode<T>(item);
ptr->data = item;
ptr->next = NULL;
if (qFront == NULL)
{
qFront = ptr;
}
else
{
lagPtr = qFront;
while (lagPtr->next!=NULL)
lagPtr = lagPtr->next;
lagPtr->next = ptr;
}
}
#endif /* QUEUE_H */
Driver Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include "Queue.h"
#include <iostream>
#include <stdexcept>
using std::cout;
using std::endl;
using std::out_of_range;
int main()
{
cout << "Testing default constructor\n\n" ;
Queue<int > q1;
cout << "q1 (size " << q1.size() << "): " << q1 << endl;
cout << "q1 is " << ((q1.empty()) ? "empty\n" : "not empty\n" );
cout << endl;
cout << "Testing push()\n\n" ;
q1.push(17);
cout << "q1 (size " << q1.size() << "): " << q1 << endl; //causes
// segmentation fault
cout << "q1 is " << ((q1.empty()) ? "empty\n" : "not empty\n" );
cout << endl;
Last edited on Nov 3, 2011 at 2:27pm Nov 3, 2011 at 2:27pm UTC
Nov 3, 2011 at 3:26am Nov 3, 2011 at 3:26am UTC
Well, this depends on the T
. If it allows assignment, then all you have to do is this:
data = newData;
Nov 3, 2011 at 4:02am Nov 3, 2011 at 4:02am UTC
1 2 3 4 5 6 7 8
else
{
lagPtr = qFront;
while (ptr->next!=NULL)
lagPtr = lagPtr->next;
lagPtr->next = ptr;
}
You meant
lagPtr ->next!=NULL
instead in your loop condition.
Last edited on Nov 3, 2011 at 4:02am Nov 3, 2011 at 4:02am UTC
Nov 3, 2011 at 4:28am Nov 3, 2011 at 4:28am UTC
I still get a segmentation fault with the changed you mentioned. I think its stuck in the decision statement. Is it because the list is some how never reaches NULL?
Last edited on Nov 3, 2011 at 5:18am Nov 3, 2011 at 5:18am UTC