Message-ID: <007f01bdf31d$ffc147e0$ef6195cc@uic> From: "Andrew Deren" To: "DJGPP" Subject: Templates Problem Date: Thu, 8 Oct 1998 19:44:39 -0500 X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 4.72.3110.1 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 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 `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; } ///////////////////////////////////////////////////////////////