Error says that it can't convert 'double*' to 'double' when alocating space

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

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
/* 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 (const char *)
{
	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.
Last edited on
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.
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
#include <iostream>
using namespace std;

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(T item, Node<T> *next=nullptr, Node<T> *prev=nullptr)
	    : data(item), pNext(next), pPrev(prev) {}
};

template <class T>
class List {
    Node<T>* head;
    Node<T>* tail;
    int numItems;
public:
    List() : head(nullptr), tail(nullptr), numItems(0) {}
    void print() {
        for (Node<T> *nd = head; nd; nd = nd->pNext)
            cout << nd->data << ' ';
        cout << '\n';
    }
    int size() { return numItems; }
    void push_back(const T& t) throw (const char *);
};

template <class T>
void List<T>::push_back(const T& t) throw (const char *) {
	try {
		Node<T> *nd = new Node<T>(t, nullptr, tail);
		if (tail) tail->pNext = nd;
		else      head = nd;
		tail = nd;
	}
	catch (std::bad_alloc) {
		throw "ERROR: Unable to allocate new nodes for list\n";
	}
	numItems++;
}

int main() {
    List<int> x;
    for (int i = 0; i < 10; i++)
        x.push_back(i);
    x.print();
}

Last edited on
Ah, I get it. Thanks for the help, tpb!
Topic archived. No new replies allowed.