Mail Archives: djgpp/1998/10/08/22:47:22
I have created a simple linked list template.
However, compilation goes fine, but when linking
I get errors:
c:/djgpp/tmp/ccaaaaaa1.o(.text+0x24):test.cpp: undefined reference to
`List<int>::List(void)'
c:/djgpp/tmp/ccaaaaaa1.o(.text+0x34):test.cpp: undefined reference to
`List<int>::add(int *)'
I hope nobody minds posting the source here.
Can anyone help me with that, please.
Thank you.
///////////////////////////////////////////////////////////////
// FILE: list.h
///////////////////////////////////////////////////////////////
#ifndef __LIST_H
#define __LIST_H
// a single node
template <class ListType>
class Node
{
Node(void):next(NULL), prev(NULL), elem(NULL) {}
Node<ListType>* next;
Node<ListType>* prev;
ListType* elem;
};
template <class ListType>
class List
{
public:
List(void);
~List(void);
bool add(ListType* new_elem);
int get_count(void) const { return count;}
// sets the current to head, call get_next to start getting elements
void start_loop(void) {current = head;}
// get current element and advance current to next.
// if there are no more element null is returned
ListType* get_next(void);
// removes all the links and elements that those
// links refer to
void delete_elements(void);
// get a value at index (1 is the first one)
ListType* get_at(int index);
protected:
int count;
Node<ListType> *head;
Node<ListType> *tail;
Node<ListType> *current;
};
#endif // __LIST_H
///////////////////////////////////////////////////////////////
// FILE: list.cpp
///////////////////////////////////////////////////////////////
template <class ListType>
List<ListType>::List(void)
:head(NULL), tail(NULL), current(NULL), count(0)
{
}
// remove all links (does not remove instances of
// the classes that are contained in the list.
// Use delete_elements to do that
template <class ListType>
List<ListType>::~List(void)
{
// delete all links
while (head) {
current = head->next;
delete head;
head = current;
}
}
template <class ListType>
bool List<ListType>::add(ListType* new_elem)
{
tail->next = new Node<ListType>();
if (!tail->next)
return false;
tail = tail->next;
tail->elem = new_elem;
count++;
return true;
}
template <class ListType>
void List<ListType>::delete_elements(void)
{
// delete all links
while (head) {
current = head->next;
delete head->elem;
delete head;
head = current;
}
head = tail = current = NULL;
count = 0;
}
// get current element and advance current to next.
// if there are no more element null is returned
template <class ListType>
ListType* List<ListType>::get_next(void)
{
ListType* temp = NULL;
if (current) {
temp = current->elem;
current = current->next;
}
return temp;
}
// get a value at index (1 is the first one)
template <class ListType>
ListType* List<ListType>::get_at(int index)
{
int i = 1;
Node<ListType>* temp = head;
while (temp) {
if (i == index)
return temp->elem;
i++;
temp = temp->next;
}
// could not find a selection at index (out of range)
return NULL;
}
///////////////////////////////////////////////////////////////
// FILE: test.cpp (just for testing)
///////////////////////////////////////////////////////////////
#include <stdio.h>
#include "list.h"
int main(int argc, char** argv)
{
int test1 = 3;
int test2 = 5;
int test3 = 7;
List<int> list;
list.add(&test1);
list.add(&test2);
list.add(&test3);
list.start_loop();
int *temp;
while ((temp = list.get_next()) != NULL) {
printf("%d ", *temp);
}
return 0;
}
///////////////////////////////////////////////////////////////
- Raw text -