In my List class, I'm trying to allocate new space for the data of the head and tail nodes, trying to see if that fixes the segmentation fault issue in my front() function; and all I got was an error saying it couldn't convert from a double* to a double in assignment. The error occurs in my push_back() function. I showed where it is.
Note that the three private variables in the List class are:
Node<T>* head
Node<T>* tail
int numItems
/* The node class */
template <class T>
class Node
{
public:
T data; // Data in each node
Node<T>* pNext; // Pointer to next node
Node<T>* pPrev; // Pointer to previous node
Node() { pNext = NULL; pPrev = NULL; } // Create a new node
Node(T item) { data = item; } // Node with data initialized to item
Node(T item, Node<T>* next); // Initialize item and next pointer
};
/* This is the push_back function where errors are occurring. */
template <class T>
void List <T> :: push_back(const T & t) throw (constchar *)
{
Node <T> * a;
try {
Node <T> * a = new Node <T> (t);
tail = new Node <T>;
tail->pPrev = new Node <T>;
tail->data = new T; //<-- Error occurs here.
head = new Node <T>;
head->pNext = new Node <T>;
head->data = new T; //<-- Error occurs here too.
}
catch (std::bad_alloc) {
throw"ERROR: Unable to allocate new nodes for list\n";
}
//a->pPrev = tail->pPrev; //Disabled because of segfaults
//a->pNext = tail; //...
tail = a;
//a->pPrev->pNext = a; //Disabled because of segfaults
if (numItems == 0)
{
head = a;
}
numItems++;
}
If you need any more code, just tell me. I haven't posted here before, so this is new to me.
push_back is supposed to add a single node to the list, but your code allocates five new nodes! You should only be creating one. And you allocate 2 T's for the data, but you should not be allocating any since T data is not a pointer.