Mail Archives: djgpp/1998/10/09/06:12:25
Newsgroups: | comp.os.msdos.djgpp
|
From: | Elliott Oti <oti AT phys DOT uu DOT nl>
|
Subject: | Re: Templates Problem
|
Sender: | usenet AT phys DOT uu DOT nl (News system Tijgertje)
|
Message-ID: | <Pine.OSF.4.03.9810091105480.5156-100000@ruunf0.phys.uu.nl>
|
In-Reply-To: | <007f01bdf31d$ffc147e0$ef6195cc@uic>
|
Date: | Fri, 9 Oct 1998 09:08:02 GMT
|
X-Nntp-Posting-Host: | ruunf0.phys.uu.nl
|
References: | <007f01bdf31d$ffc147e0$ef6195cc AT uic>
|
Mime-Version: | 1.0
|
Organization: | Physics and Astronomy, University of Utrecht, The Netherlands
|
Lines: | 210
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Reply-To: | djgpp AT delorie DOT com
|
On Thu, 8 Oct 1998, Andrew Deren wrote:
The error lies in the fact that list.cpp is not visible to test.cpp.
Templates are like macros in that the compiler can generate no code for
them unless it sees exactly what that template is supposed to be doing.
Shove all the stuff from list.cpp into list.h, delete list.cpp, and try
again.
Cheers,
Elliott Oti
> 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;
> }
>
> ///////////////////////////////////////////////////////////////
>
>
>
>
>
Elliott Oti
http://www.fys.ruu.nl/~oti
Eh? Where's my sig?
- Raw text -