954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Doubly Linked List Template Error Help

I am attempting to program a doubly linked list template. My program is setup with the class node defined in a header file,the list itself in a header file and finally the main in a separate cpp file which calls the list header. The list header calls the node header. The error I am receiving is
"dbList::dbList(int, Node*, Node*)", referenced from:
_main in dbList.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

Here is the code below:

template<class dtype>
class Node
{
public:
int position;
dtype data;
Node<dtype>* next;
Node<dtype>* prev;
Node( dtype data, Node<dtype>* next=0, Node<dtype>* prev=0);
};
#include "dbNode.h"
#include<iostream>
using namespace std;

template <typename Type> 
class dbList
{
private:
	int size;
	Node<Type>* head;
	Node<Type>* tail;
	
public:
	dbList(int size=0,Node<Type>* head=0,Node<Type>* tail=0); 
	void insertNode(Type v);
	void insertNode(Type v, int pos);
	void displayList();
	void tester();
};

template <typename Type>
void dbList<Type>::insertNode(Type v)
{
	Node<Type>* tempa;
	Node<Type>* tempb;
	Node<Type>* tempc;
	tempa->data=v;
	
	if(this->head==0)
	{
		this ->head = tempa;
		this ->head ->next = 0;
		this ->head ->prev = 0;
		this ->size++;
		this ->head->position=size;
	}
	
	else  
	{
		tempb = this ->head;
	if(tempb ->next == 0)
	{	
		tempa->next = tempb->next;
		tempa ->prev = tempb;
		tempb ->next = tempa;
		this ->tail = tempa;
		this ->size++;
		this ->tail->position=size;
	}
	
	else
	{
		tempc = this ->tail;
		tempa ->next = tempc ->next;
		tempa ->prev = tempc;
		tempc ->next = tempa;
		this ->tail = tempa;
		this ->size++;
		tempc->position=size;
	}
	
	}
}
template<typename Type>
void dbList<Type>::displayList()
{
	Node<Type> *tempa;
	tempa = this ->head;
	if(tempa == 0)
	{
		cout<<"This list is empty";
	}
	else
	{
		cout<<"List- ";
		while(tempa != 0)
		{
			cout<< tempa -> data;
			cout<< " ";
			tempa = tempa -> next;
		}
		cout<< "\n";
	}
}
template<typename Type>
void dbList<Type>::tester()
{
int selection;
Type tempa;
cout << "What would you like to do\n";
cout << "1. Insert Item\n"<<"2.Print List\n"; 
cin>>selection;
cout<<"\n";
switch(selection)
{
	case 1:cout<<"What value would you like to add?";
		cin>>tempa;
		cout<<"\n";
		insertNode(tempa);
		break;
		
	case 2:dbList<Type>::displayList();
		break;
}
}
#include"dbList.h"
#include <iostream>
using namespace std;



int main () 
{
	dbList<int> intlist;
	intlist.tester();

}


This is my first time using templates. Please point out anything that does not make sense at all. I've been going through "C++ Templates the complete guide" , but it really is not very clear to me in some spots. So any help would be greatly appreciated. Thank you for your time.

rgpii
Newbie Poster
22 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 
dbList(int size=0,Node<Type>* head=0,Node<Type>* tail=0);


it seems that you only declare that method. you have to implement it.

NicAx64
Posting Pro
536 posts since Mar 2009
Reputation Points: 86
Solved Threads: 43
 

That was supposed to be the constructor. It was the syntax I saw other templates using. Is that incorrect?

I just changed

dbList(int size=0,Node<Type>* head=0,Node<Type>* tail=0);


to

dbList()
{
int size=0;
Node<Type>* head=0;
Node<Type>* tail=0;
}


It compiled but those variables were unused and the tester method sayd Bus Error when I try to input values. Any thoughts? I mean obviously the constructor is not working in the traditional form like I just used because the variables arent being implemented at all

rgpii
Newbie Poster
22 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 
template <typename Type>
void dbList<Type>::insertNode(Type v)
{
	Node<Type>* tempa;
	Node<Type>* tempb;
	Node<Type>* tempc;
	tempa->data=v;
	
	if(this->head==0)
	{
		this ->head = tempa;
		this ->head ->next = 0;
		this ->head ->prev = 0;
		this ->size++;
		this ->head->position=size;
	}
	
	else  
	{
		tempb = this ->head;
	if(tempb ->next == 0)
	{	
		tempa->next = tempb->next;
		tempa ->prev = tempb;
		tempb ->next = tempa;
		this ->tail = tempa;
		this ->size++;
		this ->tail->position=size;
	}
	
	else
	{
		tempc = this ->tail;
		tempa ->next = tempc ->next;
		tempa ->prev = tempc;
		tempc ->next = tempa;
		this ->tail = tempa;
		this ->size++;
		tempc->position=size;
	}
	
	}
}


line 7: should be the first segmentation fault since 'tempa' is not pointing to anything and you try to deference it and assign value to 'data'

line11: this->head = tempa is also wrong, tempa is nothing, and you are assigning this->head to nothing, when you call display it'll probably crash again.

Probable solution:
Remove line 7.
Before you assign 'tempa' to this->head, create a new node and point tempa to it and then assign value to 'data'

something like

temp = new Node<int>();
Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

First, thank you for all of the replies. Those combined with the generic doubly linked list code snippet has actually allowed my code to compile. I am now receiving a bus error and I am at a complete loss as to why this is happening. Here is my new code.

#include<iostream>
using namespace std;

template <typename Type> 
class dbList
{
public:
	struct Node
	{
		Type data;
		Type position;
		Node* prev;
		Node* next;
		Node(Type t, Node* p, Node* n) : data(t), prev(p), next(n) {}//Could you please explain the purpose this line here? Why am I initializing prev and next to p and n
	};
	
	dbList(): size(0), head(0), tail (0) {} 
	void insertNode(Type v);
	void insertNode(Type v, int pos);
	void displayList();
	void tester();

private:

	int  size;
	Node* head;
	Node* tail;
	
};

template <typename Type>
void dbList<Type>::insertNode(Type v)
{
	Node* tempa;
	Node* tempb;
	Node* tempc;
	tempa->data=v;
	
	if(this->head==0)
	{
		this ->head = tempa;
		this ->head ->next = 0;
		this ->head ->prev = 0;
		this ->size++;
		this ->head->position=size;
	}
	
	else  
	{
		tempb = this ->head;
	if(tempb ->next == 0)
	{	
		tempa->next = tempb->next;
		tempa ->prev = tempb;
		tempb ->next = tempa;
		this ->tail = tempa;
		this ->size++;
		this ->tail->position=size;
	}
	
	else
	{
		tempc = this ->tail;
		tempa ->next = tempc ->next;
		tempa ->prev = tempc;
		tempc ->next = tempa;
		this ->tail = tempa;
		this ->size++;
		tempc->position=size;
	}
	
	}
}
template<typename Type>
void dbList<Type>::displayList()
{
	Node *tempa;
	tempa = this ->head;
	if(tempa == 0)
	{
		cout<<"This list is empty";
	}
	else
	{
		cout<<"List- ";
		while(tempa != 0)
		{
			cout<< tempa -> data;
			cout<< " ";
			tempa = tempa -> next;
		}
		cout<< "\n";
	}
}
template<typename Type>
void dbList<Type>::tester()
{
int tester=0;
int selection;
Type tempa;
while(tester==0)
{
	
cout << "What would you like to do\n";
cout << "9. Exit\n" << "1. Insert Item\n"<<"2.Print List\n"; 
cin>>selection;
cout<<"\n";


switch(selection)
{
	case 1:cout<<"What value would you like to add?";
		cout<< "\n";
		cin>>tempa;
		cout<<"\n";
		insertNode(tempa);
		break;
		
	case 2:dbList<Type>::displayList();
		break;
	case 3:tester=1;
		break;
}
}
}
#include"dbList.h"
#include <iostream>
using namespace std;



int main () 
{
	dbList<int> intlist;
	intlist.tester();

}


I am recieving a bus error after I try to input a second value into a new node.

rgpii
Newbie Poster
22 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 
template <typename Type>
void dbList<Type>::insertNode(Type v)
{
   Node* tempa;
   Node* tempb;
   Node* tempc;
   tempa->data=v;


Where does tempa point?

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

rgpii i don't think you are reading any of the anwers carefully. I see the same mistakes in your code again, which I pointed out in my post earlier. If you don't read and apply what' been suggested then there's really no point.

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You