Newsgroups: comp.os.msdos.djgpp From: Elliott Oti Subject: Re: Templates Problem Sender: usenet AT phys DOT uu DOT nl (News system Tijgertje) Message-ID: In-Reply-To: <007f01bdf31d$ffc147e0$ef6195cc@uic> Date: Fri, 9 Oct 1998 09:08:02 GMT X-Nntp-Posting-Host: ruunf0.phys.uu.nl Content-Type: TEXT/PLAIN; charset=US-ASCII 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::List(void)' > c:/djgpp/tmp/ccaaaaaa1.o(.text+0x34):test.cpp: undefined reference to > `List::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 Node > { > Node(void):next(NULL), prev(NULL), elem(NULL) {} > > Node* next; > Node* prev; > ListType* elem; > }; > > template > 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 *head; > Node *tail; > Node *current; > }; > > > #endif // __LIST_H > > /////////////////////////////////////////////////////////////// > // FILE: list.cpp > /////////////////////////////////////////////////////////////// > template > List::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 > List::~List(void) > { > // delete all links > while (head) { > current = head->next; > delete head; > head = current; > } > } > > template > bool List::add(ListType* new_elem) > { > tail->next = new Node(); > > if (!tail->next) > return false; > > tail = tail->next; > tail->elem = new_elem; > > count++; > > return true; > } > > template > void List::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 > ListType* List::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 > ListType* List::get_at(int index) > { > int i = 1; > > Node* 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 > #include "list.h" > > > int main(int argc, char** argv) > { > int test1 = 3; > int test2 = 5; > int test3 = 7; > > List 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?