Message-Id: <199810091044.MAA51812@ieva06.lanet.lv> From: "Andris Pavenis" To: djgpp AT delorie DOT com Date: Fri, 9 Oct 1998 12:49:35 +0300 MIME-Version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Subject: Re: Templates Problem In-reply-to: <007f01bdf31d$ffc147e0$ef6195cc@uic> X-mailer: Pegasus Mail for Win32 (v3.01b) Reply-To: djgpp AT delorie DOT com From: "Andrew Deren" To: "DJGPP" Subject: Templates Problem Date sent: Thu, 8 Oct 1998 19:44:39 -0500 Send reply to: djgpp AT delorie DOT com > 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 ^^^^^^^^^^^^^^ Looks that You are using either gcc-2.8.0 or early version of DJGPP port of gcc-2.8.1. I suggest downloading the latest update of port of gcc-2.8.1 (begin of June): gcc281b.zip gpp281b.zip (and other files if You need them) Earlier versions works OK under Win9X with LFN enabled but cause problem when LFN is not available or is disabled (LFN=N) as names of temporary files gets truncated. Therefore this problem were not noticed immediatelly. > `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; > } > > /////////////////////////////////////////////////////////////// > > > >